3 # Copyright (c) 2012-2013 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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
29 ############################################################ INCLUDES
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." "$0"
34 f_include $BSDCFG_SHARE/dialog.subr
35 f_include $BSDCFG_SHARE/mustberoot.subr
36 f_include $BSDCFG_SHARE/sysrc.subr
37 f_include $BSDCFG_SHARE/startup/rcconf.subr
39 BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
40 f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
42 ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
43 [ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
45 ############################################################ GLOBALS
48 # Global map/menu-list for the main menu
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)
59 if [ "$SHOW_DEFAULT_VALUE" ]; then
63 elif [ "$SHOW_CONFIGURED" ]; then
73 ############################################################ FUNCTIONS
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).
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"
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
93 [ "$SHOW_DEFAULT_VALUE" ] && export \
94 _${var}_default="$( f_sysrc_get_default $var )"
95 [ "$SHOW_CONFIGURED" ] && export \
96 _${var}_file="$( f_sysrc_find $var )"
98 export SHOW_VALUE SHOW_DESC SHOW_DEFAULT_VALUE SHOW_CONFIGURED
99 export msg_default_value
100 echo "$var_list" | awk '
105 cur_prefix = tolower(substr($1, 1, 1))
107 if ( prefix != cur_prefix )
112 printf "%s'\'' '\''[", var
113 if ( ENVIRON["_" var "_delete"] )
118 if ( ENVIRON["SHOW_DEFAULT_VALUE"] ) {
119 default = ENVIRON["_" var "_default"]
120 gsub(/'\''/, "'\''\\'\'\''", default)
122 gsub(/'\''/, "'\''\\'\'\''", value)
123 printf ENVIRON["msg_default_value"] "; %s",
125 } else if ( ENVIRON["SHOW_CONFIGURED"] ) {
126 printf "%s", ENVIRON["_" var "_file"]
127 } else { # SHOW_VALUE (default behavior)
129 gsub(/'\''/, "'\''\\'\'\''", value)
133 if ( ENVIRON["SHOW_DESC"] ) {
134 desc = ENVIRON["_" var "_desc"]
135 gsub(/'\''/, "'\''\\'\'\''", desc)
136 printf " '\''%s'\''", desc
145 # Display the dialog(1)-based application main menu.
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'}
160 '> $msg_view_details' '$msg_view_details_desc'
161 ${SHOW_DESC:+'$msg_view_details_help'}
164 local defaultitem= # Calculated below
165 local hline="$hline_arrows_tab_enter"
168 # [Re-]Accent the menu list before incorporating it
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
177 f_shell_escape "$help" help
178 shift 3 # rcconf_var/details/help
180 shift 2 # rcconf_var/details
184 f_getvar _${rcconf_var# }_delete delete
185 if [ "$delete" ]; then
186 details="[X]${details#???}"
188 details="[ ]${details#???}"
191 # Update buffer with modified elements
193 '$rcconf_var' '$details' ${SHOW_DESC:+'$help'}" # End-Quote
195 menu_list="$menu_list $menu_buf"
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.
201 local height width rows
202 eval f_dialog_menu${SHOW_DESC:+_with_help}_size \
205 \"\$DIALOG_BACKTITLE\" \
210 # Obtain default-item from previously stored selection
211 f_dialog_default_fetch defaultitem
214 menu_choice=$( eval $DIALOG \
215 --title \"\$DIALOG_TITLE\" \
216 --backtitle \"\$DIALOG_BACKTITLE\" \
217 --hline \"\$hline\" \
219 --ok-label \"\$msg_ok\" \
220 --cancel-label \"\$msg_cancel\" \
222 --help-label \"\$msg_details\" \
223 ${SHOW_DESC:+--item-help} \
224 --default-item \"\$defaultitem\" \
225 --menu \"\$prompt\" \
226 $height $width $rows \
228 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
231 f_dialog_data_sanitize menu_choice
232 f_dialog_menutag_store "$menu_choice"
234 # Only update default-item on success
235 [ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"
240 # dialog_menu_confirm_delete $var1 [$var2 ...]
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.
246 dialog_menu_confirm_delete()
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"
252 [ $# -ge 1 ] || return $DIALOG_CANCEL
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"
259 # Not reached unless requested to delete multiple variables
261 # Generate a menu to cleanly display the variables to be deleted
263 var_list=$( for var in $*; do echo "$var"; done | sort -u )
267 echo "$var_list" | awk '
272 cur_prefix = tolower(substr($1, 1, 1))
274 if ( prefix != cur_prefix )
279 printf "%s'\'' '\'\''\n", var
283 local height width rows
284 eval f_dialog_menu_size height width rows \
286 \"\$DIALOG_BACKTITLE\" \
291 local defaultno="defaultno"
292 [ "$USE_XDIALOG" ] && defaultno="default-no"
295 --title \"\$DIALOG_TITLE\" \
296 --backtitle \"\$DIALOG_BACKTITLE\" \
297 --hline \"\$hline\" \
299 --ok-label \"\$msg_ok\" \
300 --cancel-label \"\$msg_cancel\" \
301 --menu \"\$prompt\" \
302 $height $width $rows \
306 # Menu choice ignored; status of above command returned
309 ############################################################ MAIN
311 # Incorporate rc-file if it exists
312 [ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
315 # Process command-line arguments
317 while getopts h$GETOPTS_STDARGS flag; do
319 h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;
322 shift $(( $OPTIND - 1 ))
327 f_dialog_title "$msg_delete_startup_directives"
328 f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
331 # Genreate $RCCONF_MAP of `var desc ...' per-line (see share/rcconf.subr)
332 f_dialog_info "$msg_creating_rcconf_map"
333 f_startup_rcconf_map RCCONF_MAP
335 # Generate _${var}_desc variables from $RCCONF_MAP
336 f_startup_rcconf_map_expand RCCONF_MAP
338 # Generate RCCONF_MENU_LIST from $RCCONF_MAP
342 # Launch application main menu
347 f_dialog_menutag_fetch mtag
349 if [ "$USE_XDIALOG" ]; then
350 case "$mtag" in "> $msg_view_details")
351 f_dialog_input_view_details && dialog_create_main
354 elif [ $retval -eq $DIALOG_HELP ]; then
355 # The ``Help'' button (labeled "Details") was pressed
356 f_dialog_input_view_details && dialog_create_main
360 [ $retval -eq $DIALOG_OK ] || f_die
363 "X $msg_exit_cancel") break ;;
364 "> $msg_delete_selected")
366 for var in $( f_startup_rcconf_list ); do
367 [ "$( eval echo \$_${var}_delete )" ] || continue
368 delete_vars="$delete_vars${delete_vars:+ }$var"
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 if ! err=$( f_sysrc_delete $var 2>&1 ); then
376 f_dialog_msgbox "$err\n"
384 for var in $( f_startup_rcconf_list ); do
385 setvar _${var}_delete 1
386 export _${var}_delete
390 var_list=$( set | awk -F= "
391 /$STARTUP_RCCONF_REGEX/ {
392 if (\$1 ~ /^_[[:alpha:]_][[:alnum:]_]*_delete/)
396 [ "$var_list" ] && unset $var_list
398 *) # Anything else is a variable to edit
401 # Toggle the state-variable and loop back to menu
402 if f_isset _${var}_delete; then
405 setvar _${var}_delete 1
406 export _${var}_delete
413 ################################################################################
415 ################################################################################