rcvar revision 251266
1#!/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 251266 2013-06-02 22:34:40Z 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 prompt=
69	local hline="$hline_arrows_tab_enter"
70	local defaultitem= # Calculated below
71
72	# NOTE: Using a GLOBAL here because caller will need this list to turn
73	#       the menu tag into the menu item with f_dialog_menutag2item()
74	RCVAR_MENU_LIST="
75		'X $msg_exit' '$msg_exit_this_menu'
76		              ${SHOW_DESC:+'$msg_exit_this_menu'}
77	" # END-QUOTE
78
79	if [ ! "$_RCVAR_MAP" ]; then
80		# Generate RCVAR_MAP of `rcvar dflt script desc ...' per-line
81		f_dialog_info "$msg_creating_rcvar_map"
82		RCVAR_MAP=$( f_startup_rcvar_map )
83		export RCVAR_MAP
84		export _RCVAR_MAP=1
85	fi
86
87	RCVAR_MENU_LIST="$RCVAR_MENU_LIST $(
88		. "$RC_DEFAULTS" > /dev/null
89		source_rc_confs > /dev/null
90		for rcvar in $( echo "$RCVAR_MAP" | awk '{print $1}' ); do
91			eval export $rcvar
92		done
93		export SHOW_DESC msg_default_value
94		echo "$RCVAR_MAP" | awk '
95		BEGIN {
96			prefix = ""
97			rword  = "^[[:space:]]*[^[:space:]]*[[:space:]]*"
98		}
99		{
100			cur_prefix = tolower(substr($1, 1, 1))
101			printf "'\''"
102			if ( prefix != cur_prefix )
103				prefix = cur_prefix
104			else
105				printf " "
106			rcvar   = $1
107			default = $2
108			script  = $3
109			printf "%s'\'' '\''", rcvar
110			if ( ENVIRON[rcvar] ~ /[Yy][Ee][Ss]/ )
111				printf "[X] "
112			else
113				printf "[ ] "
114			printf "%s; " ENVIRON["msg_default_value"],
115			       script, default
116			printf "'\''"
117			if ( ENVIRON["SHOW_DESC"] ) {
118				desc = $0
119				sub(rword, "", desc)
120				sub(rword, "", desc)
121				sub(rword, "", desc)
122				gsub(/'\''/, "'\''\\'\'\''", desc)
123				printf " '\''%s'\''", desc
124			}
125			printf "\n"
126		}'
127	)"
128
129	set -f # set noglob because descriptions in the $RCVAR_MENU_LIST may
130	       # contain `*' and get expanded by dialog(1). This prevents
131	       # dialog(1) from expanding wildcards in the help line.
132
133	local height width rows
134	eval f_dialog_menu${SHOW_DESC:+_with_help}_size \
135		height width rows      \
136		\"\$DIALOG_TITLE\"     \
137		\"\$DIALOG_BACKTITLE\" \
138		\"\$prompt\"           \
139		\"\$hline\"            \
140		$RCVAR_MENU_LIST
141
142	# Obtain default-item from previously stored selection
143	f_dialog_default_fetch defaultitem
144
145	local menu_choice
146	menu_choice=$( eval $DIALOG \
147		--title \"\$DIALOG_TITLE\"         \
148		--backtitle \"\$DIALOG_BACKTITLE\" \
149		--hline \"\$hline\"                \
150		--keep-tite                        \
151		--ok-label \"\$msg_ok\"            \
152		--cancel-label \"\$msg_cancel\"    \
153		${SHOW_DESC:+--item-help}          \
154		--default-item \"\$defaultitem\"   \
155		--menu \"\$prompt\"                \
156		$height $width $rows               \
157		$RCVAR_MENU_LIST                   \
158		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
159	)
160	local retval=$?
161	f_dialog_data_sanitize menu_choice
162	f_dialog_menutag_store "$menu_choice"
163	f_dialog_default_store "$menu_choice"
164	return $retval
165}
166
167############################################################ MAIN
168
169# Incorporate rc-file if it exists
170[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
171
172#
173# Process command-line arguments
174#
175while getopts h$GETOPTS_STDARGS flag; do
176	case "$flag" in
177	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
178	esac
179done
180shift $(( $OPTIND - 1 ))
181
182#
183# Initialize
184#
185f_dialog_title "$msg_toggle_startup_services"
186f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
187f_mustberoot_init
188
189while :; do
190	dialog_menu_main || f_die
191	f_dialog_menutag_fetch mtag
192
193	case "$mtag" in
194	"X $msg_exit") break ;;
195	*) # Anything else is an rcvar to toggle
196
197		rcvar="${mtag# }"
198		value=$( eval f_dialog_menutag2item${SHOW_DESC:+_with_help} \
199		         	\"\$mtag\" $RCVAR_MENU_LIST )
200
201		# Determine the new [toggled] value to use
202		case "$value" in
203		"[X]"*) value="NO";;
204		     *) value="YES";;
205		esac
206
207		err=$( f_sysrc_set "$rcvar" "$value" 2>&1 ) ||
208			f_dialog_msgbox "$err"
209	esac
210done
211
212exit $SUCCESS
213
214################################################################################
215# END
216################################################################################
217