script.subr revision 252775
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 252775 2013-07-05 06:52:07Z 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 35247280Sdteskef_include $BSDCFG_SHARE/media/any.subr 36247280Sdteskef_include $BSDCFG_SHARE/media/tcpip.subr 37252743Sdteskef_include $BSDCFG_SHARE/mustberoot.subr 38252746Sdteskef_include $BSDCFG_SHARE/networking/services.subr 39250323Sdteskef_include $BSDCFG_SHARE/packages/packages.subr 40251996Sdteskef_include $BSDCFG_SHARE/variable.subr 41245052Sdteske 42245052Sdteske############################################################ GLOBALS 43245052Sdteske 44245052SdteskeRESWORDS= 45245052Sdteske 46245052Sdteske############################################################ FUNCTIONS 47245052Sdteske 48245052Sdteske# f_resword_new $resword $function 49245052Sdteske# 50245052Sdteske# Create a new `reserved' word for scripting purposes. Reswords call pre- 51245052Sdteske# defined functions but differ from those functions in the following ways: 52245052Sdteske# 53245052Sdteske# + Reswords do not take arguments but instead get all their data from 54245052Sdteske# the environment variable namespace. 55245052Sdteske# + Unless noError is set (must be non-NULL), if calling the resword 56245052Sdteske# results in failure, the application will terminate prematurely. 57245052Sdteske# + noError is unset after each/every resword is called. 58245052Sdteske# 59245052Sdteske# Reswords should not be used in bsdconfig itself (hence the name `reserved 60245052Sdteske# word') but instead only in scripts loaded through f_script_load()). 61245052Sdteske# 62245052Sdteskef_resword_new() 63245052Sdteske{ 64245052Sdteske local resword="$1" func="$2" 65245052Sdteske [ "$resword" ] || return $FAILURE 66245052Sdteske f_dprintf "script.subr: New resWord %s -> %s" "$resword" "$func" 67245052Sdteske eval $resword\(\){ f_dispatch $func $resword\; } 68245052Sdteske RESWORDS="$RESWORDS${RESWORDS:+ }$resword" 69245052Sdteske} 70245052Sdteske 71245052Sdteske# f_dispatch $func [$resword] 72245052Sdteske# 73245052Sdteske# Wrapper function used by `reserved words' (reswords) to call other functions. 74245052Sdteske# If $noError is set and non-NULL, a failure result from $func is ignored, 75245052Sdteske# otherwise the application is prematurely terminated using f_die(). 76245052Sdteske# 77245052Sdteske# NOTE: $noError is unset after every call. 78245052Sdteske# 79245052Sdteskef_dispatch() 80245052Sdteske{ 81245052Sdteske local func="$1" resword="${2:-$1}" 82245052Sdteske f_dprintf "f_dispatch: calling resword \`%s'" "$resword" 83245052Sdteske eval $func 84247280Sdteske local retval=$? 85247280Sdteske if [ $retval -ne $SUCCESS ]; then 86247280Sdteske local _ignore_this_error 87247280Sdteske f_getvar $VAR_NO_ERROR _ignore_this_error 88245052Sdteske [ "$_ignore_this_error" ] || f_die $retval \ 89247280Sdteske "$msg_command_failed_rest_of_script_aborted" "$resword" 90247280Sdteske fi 91245052Sdteske unset $VAR_NO_ERROR 92245052Sdteske} 93245052Sdteske 94245052Sdteske# f_script_load [$file] 95245052Sdteske# 96245052Sdteske# Load a script (usually filled with reswords). If $file is missing or NULL, 97245052Sdteske# use one of the following instead (in order): 98245052Sdteske# 99245052Sdteske# $configFile 100245052Sdteske# install.cfg 101245052Sdteske# /stand/install.fg 102245052Sdteske# /tmp/install.cfg 103245052Sdteske# 104245052Sdteske# Unknown/unregistered reswords will generate sh(1) syntax errors but not cause 105245052Sdteske# premature termination. 106245052Sdteske# 107245052Sdteske# Returns success if a script was loaded and itself returned success. 108245052Sdteske# 109245052Sdteskef_script_load() 110245052Sdteske{ 111245695Sdteske local script="$1" config_file retval=$SUCCESS 112245052Sdteske 113245052Sdteske f_dprintf "f_script_load: script=[%s]" "$script" 114245052Sdteske if [ ! "$script" ]; then 115245052Sdteske f_getvar $VAR_CONFIG_FILE config_file 116245052Sdteske for script in \ 117245052Sdteske $config_file \ 118245052Sdteske install.cfg \ 119245052Sdteske /stand/install.cfg \ 120245052Sdteske /tmp/install.cfg \ 121245052Sdteske ; do 122245052Sdteske [ -e "$script" ] && break 123245052Sdteske done 124245695Sdteske fi 125245695Sdteske 126245695Sdteske local old_interactive= 127245695Sdteske f_getvar $VAR_NONINTERACTIVE old_interactive # save a copy 128245695Sdteske 129245695Sdteske # Hint to others that we're running from a script, should they care 130245695Sdteske setvar $VAR_NONINTERACTIVE yes 131245695Sdteske 132245695Sdteske if [ "$script" = "-" ]; then 133245052Sdteske f_dprintf "f_script_load: Loading script from stdin" 134245052Sdteske eval "$( cat )" 135245695Sdteske retval=$? 136245052Sdteske else 137245052Sdteske f_dprintf "f_script_load: Loading script \`%s'" "$script" 138245052Sdteske if [ ! -e "$script" ]; then 139245052Sdteske f_show_msg "$msg_unable_to_open" "$script" 140245052Sdteske return $FAILURE 141245052Sdteske fi 142245052Sdteske . "$script" 143245695Sdteske retval=$? 144245052Sdteske fi 145245695Sdteske 146245695Sdteske [ "$old_interactive" ] && 147245695Sdteske setvar $VAR_NONINTERACTIVE "$old_interactive" 148245695Sdteske 149245695Sdteske return $retval 150245052Sdteske} 151245052Sdteske 152245052Sdteske############################################################ MAIN 153245052Sdteske 154245052Sdteske# 155245052Sdteske# Reserved words meant for scripting 156245052Sdteske# 157250318Sdteske 158250318Sdteske# this file 159250318Sdteskef_resword_new loadConfig f_script_load 160250318Sdteske 161250318Sdteske# device.subr 162250318Sdteskef_resword_new deviceRescan f_device_rescan 163250318Sdteske 164250318Sdteske# media/common.subr 165250318Sdteskef_resword_new mediaOpen f_media_open 166250318Sdteskef_resword_new mediaClose f_media_close 167250318Sdteske 168250318Sdteske# media includes 169250318Sdteskef_resword_new mediaGetType f_media_get_type # media/any.subr 170250318Sdteskef_resword_new mediaSetCDROM f_media_set_cdrom # media/cdrom.subr 171250318Sdteskef_resword_new mediaSetDOS f_media_set_dos # media/dos.subr 172250318Sdteskef_resword_new mediaSetDirectory f_media_set_directory # media/directory.subr 173250318Sdteskef_resword_new mediaSetFloppy f_media_set_floppy # media/floppy.subr 174250318Sdteskef_resword_new mediaSetNFS f_media_set_nfs # media/nfs.subr 175250318Sdteskef_resword_new mediaSetUFS f_media_set_ufs # media/ufs.subr 176250318Sdteskef_resword_new mediaSetUSB f_media_set_usb # media/usb.subr 177250318Sdteskef_resword_new optionsEditor f_media_options_menu # media/options.subr 178250318Sdteskef_resword_new tcpMenuSelect f_dialog_menu_select_tcp # media/tcp.subr 179250318Sdteske 180250318Sdteske# media/ftp.subr 181247280Sdteskef_resword_new mediaSetFTP f_media_set_ftp 182247280Sdteskef_resword_new mediaSetFTPActive f_media_set_ftp_active 183247280Sdteskef_resword_new mediaSetFTPPassive f_media_set_ftp_passive 184247280Sdteskef_resword_new mediaSetFTPUserPass f_media_set_ftp_userpass 185245052Sdteske 186252112Sdteske# media/http.subr 187252112Sdteskef_resword_new mediaSetHTTP f_media_set_http 188252112Sdteske 189250318Sdteske# media/httpproxy.subr 190250318Sdteskef_resword_new mediaSetHTTPProxy f_media_set_http_proxy 191250318Sdteske 192252746Sdteske# networking/services.subr 193252746Sdteskef_resword_new configPCNFSD f_config_pcnfsd 194252746Sdteske 195250323Sdteske# packages/packages.subr 196250323Sdteskef_resword_new configPackages f_package_config 197252745Sdteskef_resword_new packageAdd f_package_add 198252775Sdteskef_resword_new packageDelete f_package_delete 199250323Sdteske 200251997Sdteske# variable.subr 201251997Sdteskef_resword_new installVarDefaults f_variable_set_defaults 202251997Sdteskef_resword_new dumpVariables f_dump_variables 203251997Sdteske 204245052Sdteskef_dprintf "%s: Successfully loaded." script.subr 205245052Sdteske 206245052Sdteskefi # ! $_SCRIPT_SUBR 207