rcvar revision 238438
1238438Sdteske#!/bin/sh 2238438Sdteske#- 3238438Sdteske# Copyright (c) 2012 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 238438 2012-07-14 03:16:57Z dteske $ 28238438Sdteske# 29238438Sdteske############################################################ INCLUDES 30238438Sdteske 31238438SdteskeBSDCFG_LIBE="/usr/libexec/bsdconfig" 32238438Sdteske. $BSDCFG_LIBE/include/common.subr || exit 1 33238438Sdteskef_include $BSDCFG_LIBE/include/dialog.subr 34238438Sdteskef_include $BSDCFG_LIBE/include/mustberoot.subr 35238438Sdteskef_include $BSDCFG_LIBE/include/sysrc.subr 36238438Sdteske 37238438SdteskeAPP_DIR="140.startup" 38238438Sdteskef_include $BSDCFG_LIBE/$APP_DIR/include/rcvar.subr 39238438Sdteskef_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr 40238438Sdteske 41238438Sdteskeipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" ) 42238438Sdteske[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm" 43238438Sdteske 44238438Sdteske############################################################ GLOBALS 45238438Sdteske 46238438Sdteske# 47238438Sdteske# Global map/menu-list for the main menu 48238438Sdteske# 49238438SdteskeRCVAR_MAP= 50238438Sdteske_RCVAR_MAP= 51238438SdteskeRCVAR_MENU_LIST= 52238438Sdteske 53238438Sdteske# 54238438Sdteske# Options 55238438Sdteske# 56238438Sdteske# Inherit SHOW_DESC value if set, otherwise default to 1 57238438Sdteske( ${SHOW_DESC?} ) > /dev/null 2>&1 || 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{ 67238438Sdteske local size 68238438Sdteske local hline="$hline_arrows_tab_enter" 69238438Sdteske local prompt="" 70238438Sdteske 71238438Sdteske RCVAR_MENU_LIST=" 72238438Sdteske 'X $msg_exit' '$msg_exit_this_menu' 73238438Sdteske ${SHOW_DESC:+'$msg_exit_this_menu'} 74238438Sdteske " # END-QUOTE 75238438Sdteske 76238438Sdteske if [ ! "$_RCVAR_MAP" ]; then 77238438Sdteske # Genreate RCVAR_MAP of `rcvar dflt script desc ...' per-line 78238438Sdteske f_dialog_info "$msg_creating_rcvar_map" 79238438Sdteske RCVAR_MAP=$( f_startup_rcvar_map ) 80238438Sdteske export RCVAR_MAP 81238438Sdteske export _RCVAR_MAP=1 82238438Sdteske fi 83238438Sdteske 84238438Sdteske RCVAR_MENU_LIST="$RCVAR_MENU_LIST $( 85238438Sdteske . "$RC_DEFAULTS" > /dev/null 86238438Sdteske source_rc_confs > /dev/null 87238438Sdteske for rcvar in $( echo "$RCVAR_MAP" | awk '{print $1}' ); do 88238438Sdteske eval export $rcvar 89238438Sdteske done 90238438Sdteske export SHOW_DESC msg_default_value 91238438Sdteske echo "$RCVAR_MAP" | awk ' 92238438Sdteske BEGIN { 93238438Sdteske prefix = "" 94238438Sdteske rword = "^[[:space:]]*[^[:space:]]*[[:space:]]*" 95238438Sdteske } 96238438Sdteske { 97238438Sdteske cur_prefix = tolower(substr($1, 1, 1)) 98238438Sdteske printf "'\''" 99238438Sdteske if ( prefix != cur_prefix ) 100238438Sdteske prefix = cur_prefix 101238438Sdteske else 102238438Sdteske printf " " 103238438Sdteske rcvar = $1 104238438Sdteske default = $2 105238438Sdteske script = $3 106238438Sdteske printf "%s'\'' '\''", rcvar 107238438Sdteske if ( ENVIRON[rcvar] ~ /[Yy][Ee][Ss]/ ) 108238438Sdteske printf "[X] " 109238438Sdteske else 110238438Sdteske printf "[ ] " 111238438Sdteske printf "%s; " ENVIRON["msg_default_value"], 112238438Sdteske script, default 113238438Sdteske printf "'\''" 114238438Sdteske if ( ENVIRON["SHOW_DESC"] ) { 115238438Sdteske desc = $0 116238438Sdteske sub(rword, "", desc) 117238438Sdteske sub(rword, "", desc) 118238438Sdteske sub(rword, "", desc) 119238438Sdteske gsub(/'\''/, "'\''\\'\'\''", desc) 120238438Sdteske printf " '\''%s'\''", desc 121238438Sdteske } 122238438Sdteske printf "\n" 123238438Sdteske }' 124238438Sdteske )" 125238438Sdteske 126238438Sdteske set -f # noglob 127238438Sdteske 128238438Sdteske size=$( eval f_dialog_menu_${SHOW_DESC:+with_help_}size \ 129238438Sdteske \"\$DIALOG_TITLE\" \ 130238438Sdteske \"\$DIALOG_BACKTITLE\" \ 131238438Sdteske \"\$prompt\" \ 132238438Sdteske \"\$hline\" \ 133238438Sdteske $RCVAR_MENU_LIST ) 134238438Sdteske 135238438Sdteske eval $DIALOG \ 136238438Sdteske --clear --title \"\$DIALOG_TITLE\" \ 137238438Sdteske --backtitle \"\$DIALOG_BACKTITLE\" \ 138238438Sdteske --hline \"\$hline\" \ 139238438Sdteske --ok-label \"\$msg_ok\" \ 140238438Sdteske --cancel-label \"\$msg_cancel\" \ 141238438Sdteske ${SHOW_DESC:+--item-help} \ 142238438Sdteske --menu \"\$prompt\" $size \ 143238438Sdteske $RCVAR_MENU_LIST \ 144238438Sdteske 2> "$DIALOG_TMPDIR/dialog.menu.$$" 145238438Sdteske} 146238438Sdteske 147238438Sdteske############################################################ MAIN 148238438Sdteske 149238438Sdteske# Incorporate rc-file if it exists 150238438Sdteske[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc" 151238438Sdteske 152238438Sdteske# 153238438Sdteske# Process command-line arguments 154238438Sdteske# 155238438Sdteskewhile getopts hSX flag; do 156238438Sdteske case "$flag" in 157238438Sdteske h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";; 158238438Sdteske esac 159238438Sdteskedone 160238438Sdteskeshift $(( $OPTIND - 1 )) 161238438Sdteske 162238438Sdteske# 163238438Sdteske# Initialize 164238438Sdteske# 165238438Sdteskef_dialog_init 166238438Sdteskef_dialog_title "$msg_toggle_startup_services" 167238438Sdteskef_dialog_backtitle "${ipgm:+bsdconfig }$pgm" 168238438Sdteskef_mustberoot_init 169238438Sdteske 170238438Sdteskewhile :; do 171238438Sdteske dialog_menu_main 172238438Sdteske retval=$? 173238438Sdteske mtag=$( f_dialog_menutag ) 174238438Sdteske 175238438Sdteske [ $retval -eq 0 ] || f_die 176238438Sdteske 177238438Sdteske case "$mtag" in 178238438Sdteske "X $msg_exit") break ;; 179238438Sdteske *) # Anything else is an rcvar to toggle 180238438Sdteske 181238438Sdteske rcvar="${mtag# }" 182238438Sdteske value=$( eval f_dialog_menutag2item${SHOW_DESC:+_with_help} \ 183238438Sdteske \"\$mtag\" $RCVAR_MENU_LIST ) 184238438Sdteske 185238438Sdteske # Determine the new [toggled] value to use 186238438Sdteske case "$value" in 187238438Sdteske "[X]"*) value="NO";; 188238438Sdteske *) value="YES";; 189238438Sdteske esac 190238438Sdteske 191238438Sdteske err=$( f_sysrc_set "$rcvar" "$value" 2>&1 ) || 192238438Sdteske f_show_msg "$err" 193238438Sdteske esac 194238438Sdteskedone 195238438Sdteske 196238438Sdteskeexit $SUCCESS 197238438Sdteske 198238438Sdteske################################################################################ 199238438Sdteske# END 200238438Sdteske################################################################################ 201