rcvar revision 252987
11558Srgrimes#!/bin/sh
21558Srgrimes#-
31558Srgrimes# Copyright (c) 2012-2013 Devin Teske
41558Srgrimes# All rights reserved.
51558Srgrimes#
61558Srgrimes# Redistribution and use in source and binary forms, with or without
71558Srgrimes# modification, are permitted provided that the following conditions
81558Srgrimes# are met:
91558Srgrimes# 1. Redistributions of source code must retain the above copyright
101558Srgrimes#    notice, this list of conditions and the following disclaimer.
111558Srgrimes# 2. Redistributions in binary form must reproduce the above copyright
121558Srgrimes#    notice, this list of conditions and the following disclaimer in the
131558Srgrimes#    documentation and/or other materials provided with the distribution.
141558Srgrimes#
151558Srgrimes# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
161558Srgrimes# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171558Srgrimes# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
181558Srgrimes# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191558Srgrimes# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201558Srgrimes# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
211558Srgrimes# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221558Srgrimes# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231558Srgrimes# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241558Srgrimes# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251558Srgrimes# SUCH DAMAGE.
261558Srgrimes#
271558Srgrimes# $FreeBSD: head/usr.sbin/bsdconfig/startup/rcvar 252987 2013-07-07 18:51:44Z dteske $
281558Srgrimes#
291558Srgrimes############################################################ INCLUDES
301558Srgrimes
311558SrgrimesBSDCFG_SHARE="/usr/share/bsdconfig"
321558Srgrimes. $BSDCFG_SHARE/common.subr || exit 1
331558Srgrimesf_dprintf "%s: loading includes..." "$0"
341558Srgrimesf_include $BSDCFG_SHARE/dialog.subr
3523675Speterf_include $BSDCFG_SHARE/mustberoot.subr
361558Srgrimesf_include $BSDCFG_SHARE/sysrc.subr
371558Srgrimesf_include $BSDCFG_SHARE/startup/rcvar.subr
381558Srgrimes
391558SrgrimesBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
4023675Speterf_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
411558Srgrimes
421558Srgrimesipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
431558Srgrimes[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
441558Srgrimes
451558Srgrimes############################################################ GLOBALS
467585Sbde
477585Sbde#
481558Srgrimes# Global map/menu-list for the main menu
4923675Speter#
5023675SpeterRCVAR_MAP=
5123675Speter_RCVAR_MAP=
521558Srgrimes
531558Srgrimes#
541558Srgrimes# Options
551558Srgrimes#
5623675Speter# Inherit SHOW_DESC value if set, otherwise default to 1
577585Sbde[ "${SHOW_DESC+set}" ] || SHOW_DESC=1
587585Sbde
591558Srgrimes############################################################ FUNCTIONS
601558Srgrimes
611558Srgrimes# dialog_menu_main
621558Srgrimes#
631558Srgrimes# Display the dialog(1)-based application main menu.
641558Srgrimes#
651558Srgrimesdialog_menu_main()
661558Srgrimes{
671558Srgrimes	local prompt=
681558Srgrimes	local menu_list="
691558Srgrimes		'X $msg_exit' '$msg_exit_this_menu'
701558Srgrimes		              ${SHOW_DESC:+'$msg_exit_this_menu'}
711558Srgrimes	" # END-QUOTE
721558Srgrimes	local hline="$hline_arrows_tab_enter"
731558Srgrimes	local defaultitem= # Calculated below
741558Srgrimes
751558Srgrimes	if [ ! "$_RCVAR_MAP" ]; then
761558Srgrimes		# Generate RCVAR_MAP of `rcvar dflt script desc ...' per-line
771558Srgrimes		f_dialog_info "$msg_creating_rcvar_map"
781558Srgrimes		RCVAR_MAP=$( f_startup_rcvar_map )
791558Srgrimes		export RCVAR_MAP
807585Sbde		export _RCVAR_MAP=1
811558Srgrimes	fi
821558Srgrimes
831558Srgrimes	menu_list="$menu_list $(
841558Srgrimes		. "$RC_DEFAULTS" > /dev/null
851558Srgrimes		source_rc_confs > /dev/null
861558Srgrimes		for rcvar in $( echo "$RCVAR_MAP" | awk '{print $1}' ); do
871558Srgrimes			eval export $rcvar
881558Srgrimes		done
891558Srgrimes		export SHOW_DESC msg_default_value
901558Srgrimes		echo "$RCVAR_MAP" | awk '
911558Srgrimes		BEGIN {
921558Srgrimes			prefix = ""
931558Srgrimes			rword  = "^[[:space:]]*[^[:space:]]*[[:space:]]*"
941558Srgrimes		}
951558Srgrimes		{
961558Srgrimes			cur_prefix = tolower(substr($1, 1, 1))
971558Srgrimes			printf "'\''"
981558Srgrimes			if ( prefix != cur_prefix )
991558Srgrimes				prefix = cur_prefix
1001558Srgrimes			else
1011558Srgrimes				printf " "
1021558Srgrimes			rcvar   = $1
1031558Srgrimes			default = $2
1041558Srgrimes			script  = $3
1051558Srgrimes			printf "%s'\'' '\''", rcvar
1061558Srgrimes			if ( ENVIRON[rcvar] ~ /[Yy][Ee][Ss]/ )
1071558Srgrimes				printf "[X] "
1081558Srgrimes			else
1091558Srgrimes				printf "[ ] "
1101558Srgrimes			printf "%s; " ENVIRON["msg_default_value"],
1111558Srgrimes			       script, default
1121558Srgrimes			printf "'\''"
1131558Srgrimes			if ( ENVIRON["SHOW_DESC"] ) {
1141558Srgrimes				desc = $0
1151558Srgrimes				sub(rword, "", desc)
1167585Sbde				sub(rword, "", desc)
1171558Srgrimes				sub(rword, "", desc)
1181558Srgrimes				gsub(/'\''/, "'\''\\'\'\''", desc)
1191558Srgrimes				printf " '\''%s'\''", desc
1201558Srgrimes			}
1211558Srgrimes			printf "\n"
1221558Srgrimes		}'
1231558Srgrimes	)"
1241558Srgrimes
1251558Srgrimes	set -f # set noglob because descriptions in the $menu_list may
12623675Speter	       # contain `*' and get expanded by dialog(1). This prevents
1271558Srgrimes	       # dialog(1) from expanding wildcards in the help line.
1281558Srgrimes
1291558Srgrimes	local height width rows
1301558Srgrimes	eval f_dialog_menu${SHOW_DESC:+_with_help}_size \
1311558Srgrimes		height width rows      \
1321558Srgrimes		\"\$DIALOG_TITLE\"     \
1331558Srgrimes		\"\$DIALOG_BACKTITLE\" \
1341558Srgrimes		\"\$prompt\"           \
1351558Srgrimes		\"\$hline\"            \
1361558Srgrimes		$menu_list
1371558Srgrimes
1381558Srgrimes	# Obtain default-item from previously stored selection
13923675Speter	f_dialog_default_fetch defaultitem
1401558Srgrimes
1411558Srgrimes	local menu_choice
1421558Srgrimes	menu_choice=$( eval $DIALOG \
1431558Srgrimes		--title \"\$DIALOG_TITLE\"         \
1441558Srgrimes		--backtitle \"\$DIALOG_BACKTITLE\" \
1451558Srgrimes		--hline \"\$hline\"                \
1461558Srgrimes		--keep-tite                        \
1471558Srgrimes		--ok-label \"\$msg_ok\"            \
1481558Srgrimes		--cancel-label \"\$msg_cancel\"    \
1491558Srgrimes		${SHOW_DESC:+--item-help}          \
1501558Srgrimes		--default-item \"\$defaultitem\"   \
1511558Srgrimes		--menu \"\$prompt\"                \
1521558Srgrimes		$height $width $rows               \
1531558Srgrimes		$menu_list                         \
1541558Srgrimes		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
1551558Srgrimes	)
15623675Speter	local retval=$?
1571558Srgrimes	f_dialog_data_sanitize menu_choice
1581558Srgrimes	f_dialog_menutag_store "$menu_choice"
1591558Srgrimes	f_dialog_default_store "$menu_choice"
1601558Srgrimes
1611558Srgrimes	if [ $retval -eq $SUCCESS ]; then
1621558Srgrimes		local item
1631558Srgrimes		item=$( eval f_dialog_menutag2item${SHOW_DESC:+_with_help} \
1641558Srgrimes		         	\"\$menu_choice\" $menu_list )
1651558Srgrimes		f_dialog_menuitem_store "$item"
1661558Srgrimes	fi
1671558Srgrimes
16823675Speter	return $retval
1691558Srgrimes}
1701558Srgrimes
1711558Srgrimes############################################################ MAIN
1721558Srgrimes
1731558Srgrimes# Incorporate rc-file if it exists
1741558Srgrimes[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
1751558Srgrimes
1761558Srgrimes#
1771558Srgrimes# Process command-line arguments
1781558Srgrimes#
1791558Srgrimeswhile getopts h$GETOPTS_STDARGS flag; do
1801558Srgrimes	case "$flag" in
1811558Srgrimes	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;
1821558Srgrimes	esac
1831558Srgrimesdone
1841558Srgrimesshift $(( $OPTIND - 1 ))
1851558Srgrimes
18623675Speter#
1871558Srgrimes# Initialize
1881558Srgrimes#
18923675Speterf_dialog_title "$msg_toggle_startup_services"
1901558Srgrimesf_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
1911558Srgrimesf_mustberoot_init
1921558Srgrimes
1931558Srgrimes#
1941558Srgrimes# Launch application main menu
1951558Srgrimes#
1961558Srgrimeswhile :; do
1971558Srgrimes	dialog_menu_main || f_die
1981558Srgrimes	f_dialog_menutag_fetch mtag
1991558Srgrimes
2001558Srgrimes	[ "$mtag" = "X $msg_exit" ] && break
2017585Sbde
2021558Srgrimes	# Anything else is an rcvar to toggle
2031558Srgrimes
2041558Srgrimes	rcvar="${mtag# }"
2051558Srgrimes	f_dialog_menuitem_fetch value
2061558Srgrimes
2071558Srgrimes	# Determine the new [toggled] value to use
2081558Srgrimes	case "$value" in
2091558Srgrimes	"[X]"*) value="NO" ;;
2101558Srgrimes	     *) value="YES"
2111558Srgrimes	esac
2121558Srgrimes
2131558Srgrimes	err=$( f_sysrc_set "$rcvar" "$value" 2>&1 ) || f_dialog_msgbox "$err"
2141558Srgrimesdone
2151558Srgrimes
2161558Srgrimesexit $SUCCESS
2171558Srgrimes
2181558Srgrimes################################################################################
2191558Srgrimes# END
2201558Srgrimes################################################################################
2211558Srgrimes