xref: /freebsd/usr.sbin/bsdconfig/startup/rcdelete (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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/rcconf.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#
49RCCONF_MAP=
50RCCONF_MENU_LIST=
51
52#
53# Options
54#
55# Inherit SHOW_DESC value if set, otherwise default to 1
56[ "${SHOW_DESC+set}" ] || SHOW_DESC=1
57# Selectively inherit SHOW_* value (in order of preference)
58if [ "$SHOW_DEFAULT_VALUE" ]; then
59	SHOW_DEFAULT_VALUE=1
60	SHOW_CONFIGURED=
61	SHOW_VALUE=
62elif [ "$SHOW_CONFIGURED" ]; then
63	SHOW_DEFAULT_VALUE=
64	SHOW_CONFIGURED=1
65	SHOW_VALUE=
66else
67	SHOW_DEFAULT_VALUE=
68	SHOW_CONFIGURED=
69	SHOW_VALUE=1
70fi
71
72############################################################ FUNCTIONS
73
74# dialog_create_main
75#
76# Create the dialog(1) main menu. Separated from dialog_menu_main (used to
77# display the menu) to speed up execution (we only call this function when
78# initializing or changing the view details).
79#
80dialog_create_main()
81{
82	# Show infobox for modes that take a while to calculate/display
83	[ "$SHOW_DEFAULT_VALUE" -o "$SHOW_CONFIGURED" ] &&
84		f_dialog_info "$msg_creating_menu_list"
85
86	RCCONF_MENU_LIST=$(
87		. "$RC_DEFAULTS" > /dev/null
88		source_rc_confs > /dev/null
89		var_list=$( f_startup_rcconf_list )
90		for var in $var_list; do
91			eval export $var
92			[ "$SHOW_DEFAULT_VALUE" ] && export \
93				_${var}_default="$( f_sysrc_get_default $var )"
94			[ "$SHOW_CONFIGURED" ] && export \
95				_${var}_file="$( f_sysrc_find $var )"
96		done
97		export SHOW_VALUE SHOW_DESC SHOW_DEFAULT_VALUE SHOW_CONFIGURED
98		export msg_default_value
99		echo "$var_list" | awk '
100		BEGIN {
101			prefix = ""
102		}
103		{
104			cur_prefix = tolower(substr($1, 1, 1))
105			printf "'\''"
106			if ( prefix != cur_prefix )
107				prefix = cur_prefix
108			else
109				printf " "
110			var = $1
111			printf "%s'\'' '\''[", var
112			if ( ENVIRON["_" var "_delete"] )
113				printf "X"
114			else
115				printf " "
116			printf "] "
117			if ( ENVIRON["SHOW_DEFAULT_VALUE"] ) {
118				default = ENVIRON["_" var "_default"]
119				gsub(/'\''/, "'\''\\'\'\''", default)
120				value = ENVIRON[var]
121				gsub(/'\''/, "'\''\\'\'\''", value)
122				printf ENVIRON["msg_default_value"] "; %s",
123				       default, value
124			} else if ( ENVIRON["SHOW_CONFIGURED"] ) {
125				printf "%s", ENVIRON["_" var "_file"]
126			} else { # SHOW_VALUE (default behavior)
127				value = ENVIRON[var]
128				gsub(/'\''/, "'\''\\'\'\''", value)
129				printf "%s", value
130			}
131			printf "'\''"
132			if ( ENVIRON["SHOW_DESC"] ) {
133				desc = ENVIRON["_" var "_desc"]
134				gsub(/'\''/, "'\''\\'\'\''", desc)
135				printf " '\''%s'\''", desc
136			}
137			printf "\n"
138		}'
139	)
140}
141
142# dialog_menu_main
143#
144# Display the dialog(1)-based application main menu.
145#
146dialog_menu_main()
147{
148	local prompt=
149	local menu_list="
150		'X $msg_exit_cancel'     '$msg_exit_cancel_desc'
151		            ${SHOW_DESC:+'$msg_exit_cancel_help'}
152		'> $msg_delete_selected' '$msg_delete_selected_desc'
153		            ${SHOW_DESC:+'$msg_delete_selected_help'}
154		'> $msg_all'             '$msg_all_desc'
155		            ${SHOW_DESC:+'$msg_all_help'}
156		'> $msg_none'            '$msg_none_desc'
157		            ${SHOW_DESC:+'$msg_none_help'}
158	${USE_XDIALOG:+
159		'> $msg_view_details'    '$msg_view_details_desc'
160		            ${SHOW_DESC:+'$msg_view_details_help'}
161	}
162	" # END-QUOTE
163	local defaultitem= # Calculated below
164	local hline="$hline_arrows_tab_enter"
165
166	#
167	# [Re-]Accent the menu list before incorporating it
168	#
169	local rcconf_var details help menu_buf delete
170	eval set -- $RCCONF_MENU_LIST
171	while [ $# -gt 0 ]; do
172		rcconf_var="$1" details="$2" delete=
173		f_shell_escape "$details" details
174		if [ "$SHOW_DESC" ]; then
175			help="$3"
176			f_shell_escape "$help" help
177			shift 3 # rcconf_var/details/help
178		else
179			shift 2 # rcconf_var/details
180		fi
181
182		# Update mark
183		f_getvar _${rcconf_var# }_delete delete
184		if [ "$delete" ]; then
185			details="[X]${details#???}"
186		else
187			details="[ ]${details#???}"
188		fi
189
190		# Update buffer with modified elements
191		menu_buf="$menu_buf
192		'$rcconf_var' '$details' ${SHOW_DESC:+'$help'}" # End-Quote
193	done
194	menu_list="$menu_list $menu_buf"
195
196	set -f # set noglob because descriptions in the $menu_list may contain
197	       # `*' and get expanded by dialog(1) (doesn't affect Xdialog(1)).
198	       # This prevents dialog(1) from expanding wildcards in help line.
199
200	local height width rows
201	eval f_dialog_menu${SHOW_DESC:+_with_help}_size \
202		height width rows      \
203		\"\$DIALOG_TITLE\"     \
204		\"\$DIALOG_BACKTITLE\" \
205		\"\$prompt\"           \
206		\"\$hline\"            \
207		$menu_list
208
209	# Obtain default-item from previously stored selection
210	f_dialog_default_fetch defaultitem
211
212	local menu_choice
213	menu_choice=$( eval $DIALOG \
214		--title \"\$DIALOG_TITLE\"         \
215		--backtitle \"\$DIALOG_BACKTITLE\" \
216		--hline \"\$hline\"                \
217		--keep-tite                        \
218		--ok-label \"\$msg_ok\"            \
219		--cancel-label \"\$msg_cancel\"    \
220		--help-button                      \
221		--help-label \"\$msg_details\"     \
222		${SHOW_DESC:+--item-help}          \
223		--default-item \"\$defaultitem\"   \
224		--menu \"\$prompt\"                \
225		$height $width $rows               \
226		$menu_list                         \
227		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
228	)
229	local retval=$?
230	f_dialog_data_sanitize menu_choice
231	f_dialog_menutag_store "$menu_choice"
232
233	# Only update default-item on success
234	[ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"
235
236	return $retval
237}
238
239# dialog_menu_confirm_delete $var1 [$var2 ...]
240#
241# Get the user's blessing to delete one or more variables. Returns success if
242# (and only-if) the user confirms (does not press ESC or Cancel/NO). Does NOT
243# return the user's menu-choice.
244#
245dialog_menu_confirm_delete()
246{
247	local prompt="$msg_are_you_sure_you_want_delete_the_following"
248	local menu_list # Calculated below
249	local hline="$hline_arrows_tab_enter"
250
251	[ $# -ge 1 ] || return $DIALOG_CANCEL
252
253	# If asked to delete only one variable, simply ask and return
254	if [ $# -eq 1 ]; then
255		f_noyes "$msg_are_you_sure_you_want_to_delete" "$1"
256		return $?
257	fi
258	# Not reached unless requested to delete multiple variables
259
260	# Generate a menu to cleanly display the variables to be deleted
261	local var_list
262	var_list=$( for var in $*; do echo "$var"; done | sort -u )
263	menu_list=$(
264		. "$RC_DEFAULTS"
265		source_rc_confs
266		echo "$var_list" | awk '
267		BEGIN {
268			prefix = ""
269		}
270		{
271			cur_prefix = tolower(substr($1, 1, 1))
272			printf "'\''"
273			if ( prefix != cur_prefix )
274				prefix = cur_prefix
275			else
276				printf " "
277			var = $1
278			printf "%s'\'' '\'\''\n", var
279		}'
280	)
281
282	local height width rows
283	eval f_dialog_menu_size height width rows \
284	                        \"\$DIALOG_TITLE\"     \
285	                        \"\$DIALOG_BACKTITLE\" \
286	                        \"\$prompt\"           \
287	                        \"\$hline\"            \
288	                        $menu_list
289
290	local defaultno="defaultno"
291	[ "$USE_XDIALOG" ] && defaultno="default-no"
292
293	eval $DIALOG \
294		--title \"\$DIALOG_TITLE\"         \
295		--backtitle \"\$DIALOG_BACKTITLE\" \
296		--hline \"\$hline\"                \
297		--$defaultno                       \
298		--ok-label \"\$msg_ok\"            \
299		--cancel-label \"\$msg_cancel\"    \
300		--menu \"\$prompt\"                \
301		$height $width $rows               \
302		$menu_list                         \
303		2> /dev/null
304
305	# Menu choice ignored; status of above command returned
306}
307
308############################################################ MAIN
309
310# Incorporate rc-file if it exists
311[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
312
313#
314# Process command-line arguments
315#
316while getopts h$GETOPTS_STDARGS flag; do
317	case "$flag" in
318	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;
319	esac
320done
321shift $(( $OPTIND - 1 ))
322
323#
324# Initialize
325#
326f_dialog_title "$msg_delete_startup_directives"
327f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
328f_mustberoot_init
329
330# Genreate $RCCONF_MAP of `var desc ...' per-line (see share/rcconf.subr)
331f_dialog_info "$msg_creating_rcconf_map"
332f_startup_rcconf_map RCCONF_MAP
333
334# Generate _${var}_desc variables from $RCCONF_MAP
335f_startup_rcconf_map_expand RCCONF_MAP
336
337# Generate RCCONF_MENU_LIST from $RCCONF_MAP
338dialog_create_main
339
340#
341# Launch application main menu
342#
343while :; do
344	dialog_menu_main
345	retval=$?
346	f_dialog_menutag_fetch mtag
347
348	if [ "$USE_XDIALOG" ]; then
349		case "$mtag" in "> $msg_view_details")
350			f_dialog_input_view_details && dialog_create_main
351			continue
352		esac
353	elif [ $retval -eq $DIALOG_HELP ]; then
354		# The ``Help'' button (labeled "Details") was pressed
355		f_dialog_input_view_details && dialog_create_main
356		continue
357	fi
358
359	[ $retval -eq $DIALOG_OK ] || f_die
360
361	case "$mtag" in
362	"X $msg_exit_cancel") break ;;
363	"> $msg_delete_selected")
364		delete_vars=
365		for var in $( f_startup_rcconf_list ); do
366			f_getvar _${var}_delete _delete
367			[ "$_delete" ] || continue
368			delete_vars="$delete_vars $var"
369		done
370		if dialog_menu_confirm_delete $delete_vars; then
371			f_dialog_title "$msg_info"
372			f_dialog_info "$msg_deleting_selected_directives"
373			f_dialog_title_restore
374			for var in $delete_vars; do
375				f_eval_catch "$0" f_sysrc_delete \
376					'f_sysrc_delete "%s"' "$var" || break
377			done
378			dialog_create_main
379		fi
380		;;
381	"> $msg_all")
382		for var in $( f_startup_rcconf_list ); do
383			setvar _${var}_delete 1
384			export _${var}_delete
385		done
386		;;
387	"> $msg_none")
388		var_list=$( set | awk -F= "
389			/$STARTUP_RCCONF_REGEX/ {
390				if (\$1 ~ /^_[[:alpha:]_][[:alnum:]_]*_delete/)
391					print \$1
392			}"
393		)
394		[ "$var_list" ] && unset $var_list
395		;;
396	*) # Anything else is a variable to edit
397		var="${mtag# }"
398
399		# Toggle the state-variable and loop back to menu
400		if f_isset _${var}_delete; then
401			unset _${var}_delete
402		else
403			setvar _${var}_delete 1
404			export _${var}_delete
405		fi
406	esac
407done
408
409exit $SUCCESS
410
411################################################################################
412# END
413################################################################################
414