rcvar revision 251190
1151443Sharti#!/bin/sh
2#-
3# Copyright (c) 2012-2013 Devin Teske
4# All Rights Reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD: head/usr.sbin/bsdconfig/startup/rcvar 251190 2013-05-31 19:07:17Z dteske $
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." "$0"
34f_include $BSDCFG_SHARE/dialog.subr
35f_include $BSDCFG_SHARE/mustberoot.subr
36f_include $BSDCFG_SHARE/sysrc.subr
37f_include $BSDCFG_SHARE/startup/rcvar.subr
38
39BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
40f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
41
42ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
43[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
44
45############################################################ GLOBALS
46
47#
48# Global map/menu-list for the main menu
49#
50RCVAR_MAP=
51_RCVAR_MAP=
52RCVAR_MENU_LIST=
53
54#
55# Options
56#
57# Inherit SHOW_DESC value if set, otherwise default to 1
58[ "${SHOW_DESC+set}" ] || SHOW_DESC=1
59
60############################################################ FUNCTIONS
61
62# dialog_menu_main
63#
64# Display the dialog(1)-based application main menu.
65#
66dialog_menu_main()
67{
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		# Generate 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	local height width rows
129	eval f_dialog_menu${SHOW_DESC:+_with_help}_size \
130		height width rows      \
131		\"\$DIALOG_TITLE\"     \
132		\"\$DIALOG_BACKTITLE\" \
133		\"\$prompt\"           \
134		\"\$hline\"            \
135		$RCVAR_MENU_LIST
136
137	local dialog_menu
138	dialog_menu=$( eval $DIALOG \
139		--title \"\$DIALOG_TITLE\"          \
140		--backtitle \"\$DIALOG_BACKTITLE\"  \
141		--hline \"\$hline\"                 \
142		--keep-tite                         \
143		--ok-label \"\$msg_ok\"             \
144		--cancel-label \"\$msg_cancel\"     \
145		${SHOW_DESC:+--item-help}           \
146		--default-item \"\$DEFAULTITEM_$$\" \
147		--menu \"\$prompt\"                 \
148		$height $width $rows                \
149		$RCVAR_MENU_LIST                    \
150		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
151	)
152	local retval=$?
153	setvar DEFAULTITEM_$$ "$dialog_menu"
154	setvar DIALOG_MENU_$$ "$dialog_menu"
155	return $retval
156}
157
158############################################################ MAIN
159
160# Incorporate rc-file if it exists
161[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
162
163#
164# Process command-line arguments
165#
166while getopts h$GETOPTS_STDARGS flag; do
167	case "$flag" in
168	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
169	esac
170done
171shift $(( $OPTIND - 1 ))
172
173#
174# Initialize
175#
176f_dialog_title "$msg_toggle_startup_services"
177f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
178f_mustberoot_init
179
180while :; do
181	dialog_menu_main
182	retval=$?
183	mtag=$( f_dialog_menutag )
184
185	[ $retval -eq 0 ] || f_die
186
187	case "$mtag" in
188	"X $msg_exit") break ;;
189	*) # Anything else is an rcvar to toggle
190
191		rcvar="${mtag# }"
192		value=$( eval f_dialog_menutag2item${SHOW_DESC:+_with_help} \
193		         	\"\$mtag\" $RCVAR_MENU_LIST )
194
195		# Determine the new [toggled] value to use
196		case "$value" in
197		"[X]"*) value="NO";;
198		     *) value="YES";;
199		esac
200
201		err=$( f_sysrc_set "$rcvar" "$value" 2>&1 ) ||
202			f_dialog_msgbox "$err"
203	esac
204done
205
206exit $SUCCESS
207
208################################################################################
209# END
210################################################################################
211