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