rcvar revision 251361
1238438Sdteske#!/bin/sh 2238438Sdteske#- 3247278Sdteske# Copyright (c) 2012-2013 Devin Teske 4238438Sdteske# All Rights Reserved. 5238438Sdteske# 6238438Sdteske# Redistribution and use in source and binary forms, with or without 7238438Sdteske# modification, are permitted provided that the following conditions 8238438Sdteske# are met: 9238438Sdteske# 1. Redistributions of source code must retain the above copyright 10238438Sdteske# notice, this list of conditions and the following disclaimer. 11238438Sdteske# 2. Redistributions in binary form must reproduce the above copyright 12238438Sdteske# notice, this list of conditions and the following disclaimer in the 13238438Sdteske# documentation and/or other materials provided with the distribution. 14238438Sdteske# 15238438Sdteske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16238438Sdteske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE 17238438Sdteske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18238438Sdteske# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19238438Sdteske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20238438Sdteske# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21238438Sdteske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22238438Sdteske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23238438Sdteske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24238438Sdteske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25238438Sdteske# SUCH DAMAGE. 26238438Sdteske# 27238438Sdteske# $FreeBSD: head/usr.sbin/bsdconfig/startup/rcvar 251361 2013-06-04 03:30:44Z dteske $ 28238438Sdteske# 29238438Sdteske############################################################ INCLUDES 30238438Sdteske 31240684SdteskeBSDCFG_SHARE="/usr/share/bsdconfig" 32240684Sdteske. $BSDCFG_SHARE/common.subr || exit 1 33244675Sdteskef_dprintf "%s: loading includes..." "$0" 34240684Sdteskef_include $BSDCFG_SHARE/dialog.subr 35240684Sdteskef_include $BSDCFG_SHARE/mustberoot.subr 36240684Sdteskef_include $BSDCFG_SHARE/sysrc.subr 37240684Sdteskef_include $BSDCFG_SHARE/startup/rcvar.subr 38238438Sdteske 39240684SdteskeBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup" 40238438Sdteskef_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr 41238438Sdteske 42243112Sdteskeipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" ) 43238438Sdteske[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm" 44238438Sdteske 45238438Sdteske############################################################ GLOBALS 46238438Sdteske 47238438Sdteske# 48238438Sdteske# Global map/menu-list for the main menu 49238438Sdteske# 50238438SdteskeRCVAR_MAP= 51238438Sdteske_RCVAR_MAP= 52238438Sdteske 53238438Sdteske# 54238438Sdteske# Options 55238438Sdteske# 56238438Sdteske# Inherit SHOW_DESC value if set, otherwise default to 1 57240798Sdteske[ "${SHOW_DESC+set}" ] || SHOW_DESC=1 58238438Sdteske 59238438Sdteske############################################################ FUNCTIONS 60238438Sdteske 61238438Sdteske# dialog_menu_main 62238438Sdteske# 63238438Sdteske# Display the dialog(1)-based application main menu. 64238438Sdteske# 65238438Sdteskedialog_menu_main() 66238438Sdteske{ 67251264Sdteske local prompt= 68251361Sdteske local menu_list=" 69238438Sdteske 'X $msg_exit' '$msg_exit_this_menu' 70238438Sdteske ${SHOW_DESC:+'$msg_exit_this_menu'} 71238438Sdteske " # END-QUOTE 72251361Sdteske local hline="$hline_arrows_tab_enter" 73251361Sdteske local defaultitem= # Calculated below 74238438Sdteske 75238438Sdteske if [ ! "$_RCVAR_MAP" ]; then 76247278Sdteske # Generate RCVAR_MAP of `rcvar dflt script desc ...' per-line 77238438Sdteske f_dialog_info "$msg_creating_rcvar_map" 78238438Sdteske RCVAR_MAP=$( f_startup_rcvar_map ) 79238438Sdteske export RCVAR_MAP 80238438Sdteske export _RCVAR_MAP=1 81238438Sdteske fi 82238438Sdteske 83251361Sdteske menu_list="$menu_list $( 84240783Sdteske . "$RC_DEFAULTS" > /dev/null 85240783Sdteske source_rc_confs > /dev/null 86238438Sdteske for rcvar in $( echo "$RCVAR_MAP" | awk '{print $1}' ); do 87238438Sdteske eval export $rcvar 88238438Sdteske done 89238438Sdteske export SHOW_DESC msg_default_value 90238438Sdteske echo "$RCVAR_MAP" | awk ' 91238438Sdteske BEGIN { 92238438Sdteske prefix = "" 93238438Sdteske rword = "^[[:space:]]*[^[:space:]]*[[:space:]]*" 94238438Sdteske } 95238438Sdteske { 96238438Sdteske cur_prefix = tolower(substr($1, 1, 1)) 97238438Sdteske printf "'\''" 98238438Sdteske if ( prefix != cur_prefix ) 99238438Sdteske prefix = cur_prefix 100238438Sdteske else 101238438Sdteske printf " " 102238438Sdteske rcvar = $1 103238438Sdteske default = $2 104238438Sdteske script = $3 105238438Sdteske printf "%s'\'' '\''", rcvar 106238438Sdteske if ( ENVIRON[rcvar] ~ /[Yy][Ee][Ss]/ ) 107238438Sdteske printf "[X] " 108238438Sdteske else 109238438Sdteske printf "[ ] " 110238438Sdteske printf "%s; " ENVIRON["msg_default_value"], 111238438Sdteske script, default 112238438Sdteske printf "'\''" 113238438Sdteske if ( ENVIRON["SHOW_DESC"] ) { 114238438Sdteske desc = $0 115238438Sdteske sub(rword, "", desc) 116238438Sdteske sub(rword, "", desc) 117238438Sdteske sub(rword, "", desc) 118238438Sdteske gsub(/'\''/, "'\''\\'\'\''", desc) 119238438Sdteske printf " '\''%s'\''", desc 120238438Sdteske } 121238438Sdteske printf "\n" 122238438Sdteske }' 123238438Sdteske )" 124238438Sdteske 125251361Sdteske set -f # set noglob because descriptions in the $menu_list may 126251266Sdteske # contain `*' and get expanded by dialog(1). This prevents 127251266Sdteske # dialog(1) from expanding wildcards in the help line. 128238438Sdteske 129251190Sdteske local height width rows 130251190Sdteske eval f_dialog_menu${SHOW_DESC:+_with_help}_size \ 131251190Sdteske height width rows \ 132251190Sdteske \"\$DIALOG_TITLE\" \ 133251190Sdteske \"\$DIALOG_BACKTITLE\" \ 134251190Sdteske \"\$prompt\" \ 135251190Sdteske \"\$hline\" \ 136251361Sdteske $menu_list 137238438Sdteske 138251244Sdteske # Obtain default-item from previously stored selection 139251244Sdteske f_dialog_default_fetch defaultitem 140251244Sdteske 141251236Sdteske local menu_choice 142251236Sdteske menu_choice=$( eval $DIALOG \ 143251244Sdteske --title \"\$DIALOG_TITLE\" \ 144251244Sdteske --backtitle \"\$DIALOG_BACKTITLE\" \ 145251244Sdteske --hline \"\$hline\" \ 146251244Sdteske --keep-tite \ 147251244Sdteske --ok-label \"\$msg_ok\" \ 148251244Sdteske --cancel-label \"\$msg_cancel\" \ 149251244Sdteske ${SHOW_DESC:+--item-help} \ 150251244Sdteske --default-item \"\$defaultitem\" \ 151251244Sdteske --menu \"\$prompt\" \ 152251244Sdteske $height $width $rows \ 153251361Sdteske $menu_list \ 154240768Sdteske 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 155240768Sdteske ) 156240768Sdteske local retval=$? 157251236Sdteske f_dialog_data_sanitize menu_choice 158251236Sdteske f_dialog_menutag_store "$menu_choice" 159251244Sdteske f_dialog_default_store "$menu_choice" 160251361Sdteske 161251361Sdteske if [ $retval -eq $SUCCESS ]; then 162251361Sdteske local item 163251361Sdteske item=$( eval f_dialog_menutag2item${SHOW_DESC:+_with_help} \ 164251361Sdteske \"\$menu_choice\" $menu_list ) 165251361Sdteske f_dialog_menuitem_store "$item" 166251361Sdteske fi 167251361Sdteske 168240768Sdteske return $retval 169238438Sdteske} 170238438Sdteske 171238438Sdteske############################################################ MAIN 172238438Sdteske 173238438Sdteske# Incorporate rc-file if it exists 174238438Sdteske[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc" 175238438Sdteske 176238438Sdteske# 177238438Sdteske# Process command-line arguments 178238438Sdteske# 179250633Sdteskewhile getopts h$GETOPTS_STDARGS flag; do 180238438Sdteske case "$flag" in 181238438Sdteske h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";; 182238438Sdteske esac 183238438Sdteskedone 184238438Sdteskeshift $(( $OPTIND - 1 )) 185238438Sdteske 186238438Sdteske# 187238438Sdteske# Initialize 188238438Sdteske# 189238438Sdteskef_dialog_title "$msg_toggle_startup_services" 190238438Sdteskef_dialog_backtitle "${ipgm:+bsdconfig }$pgm" 191238438Sdteskef_mustberoot_init 192238438Sdteske 193238438Sdteskewhile :; do 194251236Sdteske dialog_menu_main || f_die 195251236Sdteske f_dialog_menutag_fetch mtag 196238438Sdteske 197238438Sdteske case "$mtag" in 198238438Sdteske "X $msg_exit") break ;; 199238438Sdteske *) # Anything else is an rcvar to toggle 200238438Sdteske rcvar="${mtag# }" 201251361Sdteske f_dialog_menuitem_fetch value 202238438Sdteske 203238438Sdteske # Determine the new [toggled] value to use 204238438Sdteske case "$value" in 205238438Sdteske "[X]"*) value="NO";; 206238438Sdteske *) value="YES";; 207238438Sdteske esac 208238438Sdteske 209238438Sdteske err=$( f_sysrc_set "$rcvar" "$value" 2>&1 ) || 210245437Sdteske f_dialog_msgbox "$err" 211238438Sdteske esac 212238438Sdteskedone 213238438Sdteske 214238438Sdteskeexit $SUCCESS 215238438Sdteske 216238438Sdteske################################################################################ 217238438Sdteske# END 218238438Sdteske################################################################################ 219