rcvar revision 240768
1219508Snwhitehorn#!/bin/sh
2219508Snwhitehorn#-
3219508Snwhitehorn# Copyright (c) 2012 Devin Teske
4219508Snwhitehorn# All Rights Reserved.
5219508Snwhitehorn#
6219508Snwhitehorn# Redistribution and use in source and binary forms, with or without
7219508Snwhitehorn# modification, are permitted provided that the following conditions
8219508Snwhitehorn# are met:
9219508Snwhitehorn# 1. Redistributions of source code must retain the above copyright
10219508Snwhitehorn#    notice, this list of conditions and the following disclaimer.
11219508Snwhitehorn# 2. Redistributions in binary form must reproduce the above copyright
12219508Snwhitehorn#    notice, this list of conditions and the following disclaimer in the
13219508Snwhitehorn#    documentation and/or other materials provided with the distribution.
14219508Snwhitehorn#
15219508Snwhitehorn# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16219508Snwhitehorn# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17219508Snwhitehorn# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18219508Snwhitehorn# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19219508Snwhitehorn# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20219508Snwhitehorn# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21219508Snwhitehorn# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22219508Snwhitehorn# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23219508Snwhitehorn# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24219508Snwhitehorn# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25219508Snwhitehorn# SUCH DAMAGE.
26219508Snwhitehorn#
27219508Snwhitehorn# $FreeBSD: head/usr.sbin/bsdconfig/startup/rcvar 240768 2012-09-20 23:44:13Z dteske $
28219508Snwhitehorn#
29219508Snwhitehorn############################################################ INCLUDES
30219508Snwhitehorn
31219508SnwhitehornBSDCFG_SHARE="/usr/share/bsdconfig"
32219508Snwhitehorn. $BSDCFG_SHARE/common.subr || exit 1
33219508Snwhitehornf_include $BSDCFG_SHARE/dialog.subr
34219508Snwhitehornf_include $BSDCFG_SHARE/mustberoot.subr
35219508Snwhitehornf_include $BSDCFG_SHARE/sysrc.subr
36219508Snwhitehornf_include $BSDCFG_SHARE/startup/rcvar.subr
37219508Snwhitehorn
38219508SnwhitehornBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
39219508Snwhitehornf_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
40219508Snwhitehorn
41219508Snwhitehornipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
42219508Snwhitehorn[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
43219508Snwhitehorn
44219508Snwhitehorn############################################################ GLOBALS
45219508Snwhitehorn
46219508Snwhitehorn#
47219508Snwhitehorn# Global map/menu-list for the main menu
48#
49RCVAR_MAP=
50_RCVAR_MAP=
51RCVAR_MENU_LIST=
52
53#
54# Options
55#
56# Inherit SHOW_DESC value if set, otherwise default to 1
57( ${SHOW_DESC?} ) > /dev/null 2>&1 || SHOW_DESC=1
58
59############################################################ FUNCTIONS
60
61# dialog_menu_main
62#
63# Display the dialog(1)-based application main menu.
64#
65dialog_menu_main()
66{
67	local size
68	local hline="$hline_arrows_tab_enter"
69	local prompt=""
70
71	RCVAR_MENU_LIST="
72		'X $msg_exit' '$msg_exit_this_menu'
73		              ${SHOW_DESC:+'$msg_exit_this_menu'}
74	" # END-QUOTE
75
76	if [ ! "$_RCVAR_MAP" ]; then
77		# Genreate RCVAR_MAP of `rcvar dflt script desc ...' per-line
78		f_dialog_info "$msg_creating_rcvar_map"
79		RCVAR_MAP=$( f_startup_rcvar_map )
80		export RCVAR_MAP
81		export _RCVAR_MAP=1
82	fi
83
84	RCVAR_MENU_LIST="$RCVAR_MENU_LIST $(
85		. "$RC_DEFAULTS" > /dev/null
86		source_rc_confs > /dev/null
87		for rcvar in $( echo "$RCVAR_MAP" | awk '{print $1}' ); do
88			eval export $rcvar
89		done
90		export SHOW_DESC msg_default_value
91		echo "$RCVAR_MAP" | awk '
92		BEGIN {
93			prefix = ""
94			rword  = "^[[:space:]]*[^[:space:]]*[[:space:]]*"
95		}
96		{
97			cur_prefix = tolower(substr($1, 1, 1))
98			printf "'\''"
99			if ( prefix != cur_prefix )
100				prefix = cur_prefix
101			else
102				printf " "
103			rcvar   = $1
104			default = $2
105			script  = $3
106			printf "%s'\'' '\''", rcvar
107			if ( ENVIRON[rcvar] ~ /[Yy][Ee][Ss]/ )
108				printf "[X] "
109			else
110				printf "[ ] "
111			printf "%s; " ENVIRON["msg_default_value"],
112			       script, default
113			printf "'\''"
114			if ( ENVIRON["SHOW_DESC"] ) {
115				desc = $0
116				sub(rword, "", desc)
117				sub(rword, "", desc)
118				sub(rword, "", desc)
119				gsub(/'\''/, "'\''\\'\'\''", desc)
120				printf " '\''%s'\''", desc
121			}
122			printf "\n"
123		}'
124	)"
125
126	set -f # noglob
127
128	size=$( eval f_dialog_menu_${SHOW_DESC:+with_help_}size \
129	        	\"\$DIALOG_TITLE\"     \
130	        	\"\$DIALOG_BACKTITLE\" \
131	        	\"\$prompt\"           \
132	        	\"\$hline\"            \
133	        	$RCVAR_MENU_LIST       )
134
135	local dialog_menu
136	dialog_menu=$( eval $DIALOG \
137		--clear --title \"\$DIALOG_TITLE\" \
138		--backtitle \"\$DIALOG_BACKTITLE\" \
139		--hline \"\$hline\"                \
140		--ok-label \"\$msg_ok\"            \
141		--cancel-label \"\$msg_cancel\"    \
142		${SHOW_DESC:+--item-help}          \
143		--menu \"\$prompt\" $size          \
144		$RCVAR_MENU_LIST                   \
145		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
146	)
147	local retval=$?
148	setvar DIALOG_MENU_$$ "$dialog_menu"
149	return $retval
150}
151
152############################################################ MAIN
153
154# Incorporate rc-file if it exists
155[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
156
157#
158# Process command-line arguments
159#
160while getopts hSX flag; do
161	case "$flag" in
162	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
163	esac
164done
165shift $(( $OPTIND - 1 ))
166
167#
168# Initialize
169#
170f_dialog_init
171f_dialog_title "$msg_toggle_startup_services"
172f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
173f_mustberoot_init
174
175while :; do
176	dialog_menu_main
177	retval=$?
178	mtag=$( f_dialog_menutag )
179
180	[ $retval -eq 0 ] || f_die
181
182	case "$mtag" in
183	"X $msg_exit") break ;;
184	*) # Anything else is an rcvar to toggle
185
186		rcvar="${mtag# }"
187		value=$( eval f_dialog_menutag2item${SHOW_DESC:+_with_help} \
188		         	\"\$mtag\" $RCVAR_MENU_LIST )
189
190		# Determine the new [toggled] value to use
191		case "$value" in
192		"[X]"*) value="NO";;
193		     *) value="YES";;
194		esac
195
196		err=$( f_sysrc_set "$rcvar" "$value" 2>&1 ) ||
197			f_show_msg "$err"
198	esac
199done
200
201exit $SUCCESS
202
203################################################################################
204# END
205################################################################################
206