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