script.subr revision 263150
1245052Sdteskeif [ ! "$_SCRIPT_SUBR" ]; then _SCRIPT_SUBR=1 2245052Sdteske# 3247280Sdteske# Copyright (c) 2012-2014 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 (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 263150 2014-03-14 03:42:05Z 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 37250323Sdteskef_include $BSDCFG_SHARE/mustberoot.subr 38251996Sdteskef_include $BSDCFG_SHARE/networking/services.subr 39245052Sdteskef_include $BSDCFG_SHARE/packages/packages.subr 40245052Sdteskef_include $BSDCFG_SHARE/usermgmt/group.subr 41245052Sdteskef_include $BSDCFG_SHARE/usermgmt/user.subr 42245052Sdteskef_include $BSDCFG_SHARE/variable.subr 43245052Sdteske 44245052Sdteske############################################################ GLOBALS 45245052Sdteske 46245052SdteskeRESWORDS= 47245052Sdteske 48245052Sdteske############################################################ FUNCTIONS 49245052Sdteske 50245052Sdteske# f_resword_new $resword $function 51245052Sdteske# 52245052Sdteske# Create a new `reserved' word for scripting purposes. Reswords call pre- 53245052Sdteske# defined functions but differ from those functions in the following ways: 54245052Sdteske# 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" 82247280Sdteske shift 2 # func resword 83247280Sdteske f_dprintf "f_dispatch: calling resword \`%s'" "$resword" 84247280Sdteske eval $func "$@" 85247280Sdteske local retval=$? 86245052Sdteske if [ $retval -ne $SUCCESS ]; then 87247280Sdteske local _ignore_this_error 88247280Sdteske f_getvar $VAR_NO_ERROR _ignore_this_error 89245052Sdteske [ "$_ignore_this_error" ] || f_die $retval \ 90245052Sdteske "$msg_command_failed_rest_of_script_aborted" "$resword" 91245052Sdteske fi 92245052Sdteske unset $VAR_NO_ERROR 93245052Sdteske} 94245052Sdteske 95245052Sdteske# f_script_load [$file] 96245052Sdteske# 97245052Sdteske# Load a script (usually filled with reswords). If $file is missing or NULL, 98245052Sdteske# use one of the following instead (in order): 99245052Sdteske# 100245052Sdteske# $configFile (global) 101245052Sdteske# install.cfg 102245052Sdteske# /stand/install.fg 103245052Sdteske# /tmp/install.cfg 104245052Sdteske# 105245052Sdteske# Unknown/unregistered reswords will generate sh(1) syntax errors but not cause 106245052Sdteske# premature termination. 107245052Sdteske# 108245052Sdteske# Returns success if a script was loaded and itself returned success. 109245695Sdteske# 110245052Sdteskef_script_load() 111245052Sdteske{ 112245052Sdteske local funcname=f_script_load 113245052Sdteske local script="$1" config_file retval=$SUCCESS 114245052Sdteske 115245052Sdteske f_dprintf "$funcname: script=[%s]" "$script" 116245052Sdteske if [ ! "$script" ]; then 117245052Sdteske f_getvar $VAR_CONFIG_FILE config_file 118245052Sdteske for script in \ 119245052Sdteske $config_file \ 120245052Sdteske install.cfg \ 121245052Sdteske /stand/install.cfg \ 122245695Sdteske /tmp/install.cfg \ 123245695Sdteske ; do 124245695Sdteske [ -e "$script" ] && break 125245695Sdteske done 126245695Sdteske fi 127245695Sdteske 128245695Sdteske local old_interactive= 129245695Sdteske f_getvar $VAR_NONINTERACTIVE old_interactive # save a copy 130245695Sdteske 131245052Sdteske # Hint to others that we're running from a script, should they care 132245052Sdteske setvar $VAR_NONINTERACTIVE yes 133245695Sdteske 134245052Sdteske if [ "$script" = "-" ]; then 135245052Sdteske f_dprintf "$funcname: Loading script from stdin" 136245052Sdteske eval "$( cat )" 137245052Sdteske retval=$? 138245052Sdteske else 139245052Sdteske f_dprintf "$funcname: Loading script \`%s'" "$script" 140245052Sdteske if [ ! -e "$script" ]; then 141245695Sdteske f_show_msg "$msg_unable_to_open" "$script" 142245052Sdteske return $FAILURE 143245695Sdteske fi 144245695Sdteske . "$script" 145245695Sdteske retval=$? 146245695Sdteske fi 147245695Sdteske 148245052Sdteske [ "$old_interactive" ] && 149245052Sdteske setvar $VAR_NONINTERACTIVE "$old_interactive" 150245052Sdteske 151245052Sdteske return $retval 152245052Sdteske} 153245052Sdteske 154245052Sdteske############################################################ MAIN 155250318Sdteske 156250318Sdteske# 157250318Sdteske# Reserved words meant for scripting 158250318Sdteske# 159250318Sdteske 160250318Sdteske# this file 161250318Sdteskef_resword_new loadConfig f_script_load 162250318Sdteske 163250318Sdteske# device.subr 164250318Sdteskef_resword_new deviceRescan f_device_rescan 165250318Sdteske 166250318Sdteske# media/common.subr 167250318Sdteskef_resword_new mediaOpen f_media_open 168250318Sdteskef_resword_new mediaClose f_media_close 169250318Sdteske 170250318Sdteske# media includes 171250318Sdteskef_resword_new mediaGetType f_media_get_type # media/any.subr 172250318Sdteskef_resword_new mediaSetCDROM f_media_set_cdrom # media/cdrom.subr 173250318Sdteskef_resword_new mediaSetDOS f_media_set_dos # media/dos.subr 174250318Sdteskef_resword_new mediaSetDirectory f_media_set_directory # media/directory.subr 175250318Sdteskef_resword_new mediaSetFloppy f_media_set_floppy # media/floppy.subr 176250318Sdteskef_resword_new mediaSetNFS f_media_set_nfs # media/nfs.subr 177250318Sdteskef_resword_new mediaSetUFS f_media_set_ufs # media/ufs.subr 178250318Sdteskef_resword_new mediaSetUSB f_media_set_usb # media/usb.subr 179247280Sdteskef_resword_new optionsEditor f_media_options_menu # media/options.subr 180247280Sdteskef_resword_new tcpMenuSelect f_dialog_menu_select_tcp # media/tcp.subr 181247280Sdteske 182247280Sdteske# media/ftp.subr 183245052Sdteskef_resword_new mediaSetFTP f_media_set_ftp 184250318Sdteskef_resword_new mediaSetFTPActive f_media_set_ftp_active 185250318Sdteskef_resword_new mediaSetFTPPassive f_media_set_ftp_passive 186250318Sdteskef_resword_new mediaSetFTPUserPass f_media_set_ftp_userpass 187250318Sdteske 188250323Sdteske# media/http.subr 189250323Sdteskef_resword_new mediaSetHTTP f_media_set_http 190250323Sdteske 191251997Sdteske# media/httpproxy.subr 192251997Sdteskef_resword_new mediaSetHTTPProxy f_media_set_http_proxy 193251997Sdteske 194251997Sdteske# networking/services.subr 195245052Sdteskef_resword_new configPCNFSD f_config_pcnfsd 196245052Sdteske 197245052Sdteske# packages/packages.subr 198f_resword_new configPackages f_package_config 199f_resword_new packageAdd f_package_add 200f_resword_new packageDelete f_package_delete 201f_resword_new packageReinstall f_package_reinstall 202 203# usermgmt/group.subr 204f_resword_new addGroup f_group_add 205f_resword_new deleteGroup f_group_delete 206f_resword_new editGroup f_group_edit 207 208# usermgmt/user.subr 209f_resword_new addUser f_user_add 210f_resword_new deleteUser f_user_delete 211f_resword_new editUser f_user_edit 212 213# variable.subr 214f_resword_new installVarDefaults f_variable_set_defaults 215f_resword_new dumpVariables f_dump_variables 216 217f_dprintf "%s: Successfully loaded." script.subr 218 219fi # ! $_SCRIPT_SUBR 220