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