script.subr revision 262900
1245052Sdteskeif [ ! "$_SCRIPT_SUBR" ]; then _SCRIPT_SUBR=1 2245052Sdteske# 3262895Sdteske# Copyright (c) 2012-2014 Devin Teske 4252980Sdteske# 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 16252987Sdteske# 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 20252987Sdteske# DAMAGES (INCLUDING, 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 262900 2014-03-07 20:24:59Z 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# + Unless noError is set (must be non-NULL), if calling the resword 54245052Sdteske# results in failure, the application will terminate prematurely. 55245052Sdteske# + noError is unset after each/every resword is called. 56245052Sdteske# 57245052Sdteske# Reswords should not be used in bsdconfig itself (hence the name `reserved 58262895Sdteske# word') but instead only in scripts loaded through f_script_load(). 59245052Sdteske# 60245052Sdteskef_resword_new() 61245052Sdteske{ 62245052Sdteske local resword="$1" func="$2" 63245052Sdteske [ "$resword" ] || return $FAILURE 64245052Sdteske f_dprintf "script.subr: New resWord %s -> %s" "$resword" "$func" 65262895Sdteske eval $resword\(\){ f_dispatch $func $resword \"\$@\"\; } 66245052Sdteske RESWORDS="$RESWORDS${RESWORDS:+ }$resword" 67245052Sdteske} 68245052Sdteske 69262895Sdteske# f_dispatch $func $resword 70245052Sdteske# 71245052Sdteske# Wrapper function used by `reserved words' (reswords) to call other functions. 72245052Sdteske# If $noError is set and non-NULL, a failure result from $func is ignored, 73245052Sdteske# otherwise the application is prematurely terminated using f_die(). 74245052Sdteske# 75245052Sdteske# NOTE: $noError is unset after every call. 76245052Sdteske# 77245052Sdteskef_dispatch() 78245052Sdteske{ 79262895Sdteske local func="$1" resword="$2" 80262895Sdteske shift 2 # func resword 81245052Sdteske f_dprintf "f_dispatch: calling resword \`%s'" "$resword" 82262895Sdteske eval $func "$@" 83247280Sdteske local retval=$? 84247280Sdteske if [ $retval -ne $SUCCESS ]; then 85247280Sdteske local _ignore_this_error 86247280Sdteske f_getvar $VAR_NO_ERROR _ignore_this_error 87245052Sdteske [ "$_ignore_this_error" ] || f_die $retval \ 88247280Sdteske "$msg_command_failed_rest_of_script_aborted" "$resword" 89247280Sdteske fi 90245052Sdteske unset $VAR_NO_ERROR 91245052Sdteske} 92245052Sdteske 93245052Sdteske# f_script_load [$file] 94245052Sdteske# 95245052Sdteske# Load a script (usually filled with reswords). If $file is missing or NULL, 96245052Sdteske# use one of the following instead (in order): 97245052Sdteske# 98262900Sdteske# $configFile (global) 99245052Sdteske# install.cfg 100245052Sdteske# /stand/install.fg 101245052Sdteske# /tmp/install.cfg 102245052Sdteske# 103245052Sdteske# Unknown/unregistered reswords will generate sh(1) syntax errors but not cause 104245052Sdteske# premature termination. 105245052Sdteske# 106245052Sdteske# Returns success if a script was loaded and itself returned success. 107245052Sdteske# 108245052Sdteskef_script_load() 109245052Sdteske{ 110262900Sdteske local funcname=f_script_load 111245695Sdteske local script="$1" config_file retval=$SUCCESS 112245052Sdteske 113262900Sdteske f_dprintf "$funcname: 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 133262900Sdteske f_dprintf "$funcname: Loading script from stdin" 134245052Sdteske eval "$( cat )" 135245695Sdteske retval=$? 136245052Sdteske else 137262900Sdteske f_dprintf "$funcname: 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 199252844Sdteskef_resword_new packageReinstall f_package_reinstall 200250323Sdteske 201251997Sdteske# variable.subr 202251997Sdteskef_resword_new installVarDefaults f_variable_set_defaults 203251997Sdteskef_resword_new dumpVariables f_dump_variables 204251997Sdteske 205245052Sdteskef_dprintf "%s: Successfully loaded." script.subr 206245052Sdteske 207245052Sdteskefi # ! $_SCRIPT_SUBR 208