xref: /freebsd/usr.sbin/bsdconfig/startup/rcconf (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1641a6cfbSDevin Teske#!/bin/sh
2641a6cfbSDevin Teske#-
3e14ddd1fSDevin Teske# Copyright (c) 2012-2013 Devin Teske
4f8ea072aSDevin Teske# All rights reserved.
5641a6cfbSDevin Teske#
6641a6cfbSDevin Teske# Redistribution and use in source and binary forms, with or without
7641a6cfbSDevin Teske# modification, are permitted provided that the following conditions
8641a6cfbSDevin Teske# are met:
9641a6cfbSDevin Teske# 1. Redistributions of source code must retain the above copyright
10641a6cfbSDevin Teske#    notice, this list of conditions and the following disclaimer.
11641a6cfbSDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
12641a6cfbSDevin Teske#    notice, this list of conditions and the following disclaimer in the
13641a6cfbSDevin Teske#    documentation and/or other materials provided with the distribution.
14641a6cfbSDevin Teske#
15641a6cfbSDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
168e37a7c8SDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17641a6cfbSDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18641a6cfbSDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19641a6cfbSDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
208e37a7c8SDevin Teske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21641a6cfbSDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22641a6cfbSDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23641a6cfbSDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24641a6cfbSDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25641a6cfbSDevin Teske# SUCH DAMAGE.
26641a6cfbSDevin Teske#
27641a6cfbSDevin Teske#
28641a6cfbSDevin Teske############################################################ INCLUDES
29641a6cfbSDevin Teske
30ab2043b8SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
31ab2043b8SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
3256961fd7SDevin Teskef_dprintf "%s: loading includes..." "$0"
33ab2043b8SDevin Teskef_include $BSDCFG_SHARE/dialog.subr
34ab2043b8SDevin Teskef_include $BSDCFG_SHARE/mustberoot.subr
35ab2043b8SDevin Teskef_include $BSDCFG_SHARE/sysrc.subr
36ab2043b8SDevin Teskef_include $BSDCFG_SHARE/startup/rcconf.subr
37641a6cfbSDevin Teske
38ab2043b8SDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
39641a6cfbSDevin Teskef_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
40641a6cfbSDevin Teske
41*d4ae33f0SDevin Teskef_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" ipgm &&
42*d4ae33f0SDevin Teske	pgm="${ipgm:-$pgm}"
43641a6cfbSDevin Teske
44641a6cfbSDevin Teske############################################################ GLOBALS
45641a6cfbSDevin Teske
46641a6cfbSDevin Teske#
47641a6cfbSDevin Teske# Global map/menu-list for the main menu
48641a6cfbSDevin Teske#
49641a6cfbSDevin TeskeRCCONF_MAP=
50641a6cfbSDevin Teske_RCCONF_MAP=
51641a6cfbSDevin Teske
52641a6cfbSDevin Teske#
53641a6cfbSDevin Teske# Options
54641a6cfbSDevin Teske#
55641a6cfbSDevin Teske# Inherit SHOW_DESC value if set, otherwise default to 1
568d8d314dSDevin Teske[ "${SHOW_DESC+set}" ] || SHOW_DESC=1
57641a6cfbSDevin Teske# Selectively inherit SHOW_* value (in order of preference)
58641a6cfbSDevin Teskeif [ "$SHOW_DEFAULT_VALUE" ]; then
59641a6cfbSDevin Teske	SHOW_DEFAULT_VALUE=1
60641a6cfbSDevin Teske	SHOW_CONFIGURED=
61641a6cfbSDevin Teske	SHOW_VALUE=
62641a6cfbSDevin Teskeelif [ "$SHOW_CONFIGURED" ]; then
63641a6cfbSDevin Teske	SHOW_DEFAULT_VALUE=
64641a6cfbSDevin Teske	SHOW_CONFIGURED=1
65641a6cfbSDevin Teske	SHOW_VALUE=
66641a6cfbSDevin Teskeelse
67641a6cfbSDevin Teske	SHOW_DEFAULT_VALUE=
68641a6cfbSDevin Teske	SHOW_CONFIGURED=
69641a6cfbSDevin Teske	SHOW_VALUE=1
70641a6cfbSDevin Teskefi
71641a6cfbSDevin Teske
72641a6cfbSDevin Teske############################################################ FUNCTIONS
73641a6cfbSDevin Teske
74641a6cfbSDevin Teske# dialog_menu_main
75641a6cfbSDevin Teske#
76641a6cfbSDevin Teske# Display the dialog(1)-based application main menu.
77641a6cfbSDevin Teske#
78641a6cfbSDevin Teskedialog_menu_main()
79641a6cfbSDevin Teske{
80052f8969SDevin Teske	local prompt=
81052f8969SDevin Teske	local menu_list="
82641a6cfbSDevin Teske		'X $msg_exit'    '$msg_exit_desc'
83595e514dSDevin Teske		                  ${SHOW_DESC:+'$msg_exit_this_menu'}
84641a6cfbSDevin Teske		'> $msg_add_new' '$msg_add_new_desc'
85641a6cfbSDevin Teske		                  ${SHOW_DESC:+'$msg_add_new_help'}
86641a6cfbSDevin Teske		'> $msg_delete'  '$msg_delete_desc'
87641a6cfbSDevin Teske		                  ${SHOW_DESC:+'$msg_delete_help'}
88641a6cfbSDevin Teske	${USE_XDIALOG:+
89641a6cfbSDevin Teske		'> $msg_view_details' '$msg_view_details_desc'
90641a6cfbSDevin Teske		                       ${SHOW_DESC:+'$msg_view_details_help'}
91641a6cfbSDevin Teske	}
92641a6cfbSDevin Teske	" # END-QUOTE
93052f8969SDevin Teske	local defaultitem= # Calculated below
94052f8969SDevin Teske	local hline="$hline_arrows_tab_enter"
95641a6cfbSDevin Teske
96641a6cfbSDevin Teske	if [ ! "$_RCCONF_MAP" ]; then
97641a6cfbSDevin Teske		# Genreate RCCONF_MAP of `var desc ...' per-line
98641a6cfbSDevin Teske		f_dialog_info "$msg_creating_rcconf_map"
99d3a0f918SDevin Teske		f_startup_rcconf_map RCCONF_MAP
100641a6cfbSDevin Teske		export RCCONF_MAP
101641a6cfbSDevin Teske		# Generate _${var}_desc variables from $RCCONF_MAP
102d3a0f918SDevin Teske		f_startup_rcconf_map_expand RCCONF_MAP
103641a6cfbSDevin Teske		export _RCCONF_MAP=1
104641a6cfbSDevin Teske	fi
105641a6cfbSDevin Teske
106641a6cfbSDevin Teske	# Show infobox for modes that take a while to calculate/display
107641a6cfbSDevin Teske	[ "$SHOW_DEFAULT_VALUE" -o "$SHOW_CONFIGURED" ] &&
108641a6cfbSDevin Teske		f_dialog_info "$msg_creating_menu_list"
109641a6cfbSDevin Teske
110052f8969SDevin Teske	menu_list="$menu_list $(
111fb7d723eSDevin Teske		. "$RC_DEFAULTS" > /dev/null
112fb7d723eSDevin Teske		source_rc_confs > /dev/null
113641a6cfbSDevin Teske		var_list=$( f_startup_rcconf_list )
114641a6cfbSDevin Teske		for var in $var_list; do
115641a6cfbSDevin Teske			eval export $var
116641a6cfbSDevin Teske			[ "$SHOW_DEFAULT_VALUE" ] && export \
117641a6cfbSDevin Teske				_${var}_default="$( f_sysrc_get_default $var )"
118641a6cfbSDevin Teske			[ "$SHOW_CONFIGURED" ] && export \
119641a6cfbSDevin Teske				_${var}_file="$( f_sysrc_find $var )"
120641a6cfbSDevin Teske		done
121641a6cfbSDevin Teske		export SHOW_VALUE SHOW_DESC SHOW_DEFAULT_VALUE SHOW_CONFIGURED
122641a6cfbSDevin Teske		export msg_default_value
123641a6cfbSDevin Teske		echo "$var_list" | awk '
124641a6cfbSDevin Teske		BEGIN {
125641a6cfbSDevin Teske			prefix = ""
126641a6cfbSDevin Teske		}
127641a6cfbSDevin Teske		{
128641a6cfbSDevin Teske			cur_prefix = tolower(substr($1, 1, 1))
129641a6cfbSDevin Teske			printf "'\''"
130641a6cfbSDevin Teske			if ( prefix != cur_prefix )
131641a6cfbSDevin Teske				prefix = cur_prefix
132641a6cfbSDevin Teske			else
133641a6cfbSDevin Teske				printf " "
134641a6cfbSDevin Teske			var = $1
135641a6cfbSDevin Teske			printf "%s'\'' '\''", var
136641a6cfbSDevin Teske			if ( ENVIRON["SHOW_DEFAULT_VALUE"] ) {
137641a6cfbSDevin Teske				default = ENVIRON["_" var "_default"]
138641a6cfbSDevin Teske				gsub(/'\''/, "'\''\\'\'\''", default)
139641a6cfbSDevin Teske				value = ENVIRON[var]
140641a6cfbSDevin Teske				gsub(/'\''/, "'\''\\'\'\''", value)
141641a6cfbSDevin Teske				printf ENVIRON["msg_default_value"] "; %s",
142641a6cfbSDevin Teske				       default, value
143641a6cfbSDevin Teske			} else if ( ENVIRON["SHOW_CONFIGURED"] ) {
144641a6cfbSDevin Teske				printf "%s", ENVIRON["_" var "_file"]
145641a6cfbSDevin Teske			} else { # SHOW_VALUE (default behavior)
146641a6cfbSDevin Teske				value = ENVIRON[var]
147641a6cfbSDevin Teske				gsub(/'\''/, "'\''\\'\'\''", value)
148641a6cfbSDevin Teske				printf "%s", value
149641a6cfbSDevin Teske			}
150641a6cfbSDevin Teske			printf "'\''"
151641a6cfbSDevin Teske			if ( ENVIRON["SHOW_DESC"] ) {
152641a6cfbSDevin Teske				desc = ENVIRON["_" var "_desc"]
153641a6cfbSDevin Teske				gsub(/'\''/, "'\''\\'\'\''", desc)
154641a6cfbSDevin Teske				printf " '\''%s'\''", desc
155641a6cfbSDevin Teske			}
156641a6cfbSDevin Teske			printf "\n"
157641a6cfbSDevin Teske		}'
158641a6cfbSDevin Teske	)"
159641a6cfbSDevin Teske
160f4844f65SDevin Teske	set -f # set noglob because descriptions in the $menu_list may contain
161f4844f65SDevin Teske	       # `*' and get expanded by dialog(1) (doesn't affect Xdialog(1)).
162f4844f65SDevin Teske	       # This prevents dialog(1) from expanding wildcards in help line.
163641a6cfbSDevin Teske
16474036c4dSDevin Teske	local height width rows
16574036c4dSDevin Teske	eval f_dialog_menu${SHOW_DESC:+_with_help}_size \
16674036c4dSDevin Teske		height width rows      \
167641a6cfbSDevin Teske		\"\$DIALOG_TITLE\"     \
168641a6cfbSDevin Teske		\"\$DIALOG_BACKTITLE\" \
169641a6cfbSDevin Teske		\"\$prompt\"           \
170641a6cfbSDevin Teske		\"\$hline\"            \
171052f8969SDevin Teske		$menu_list
172641a6cfbSDevin Teske
17377599778SDevin Teske	# Obtain default-item from previously stored selection
17477599778SDevin Teske	f_dialog_default_fetch defaultitem
17577599778SDevin Teske
176fd962ac6SDevin Teske	local menu_choice
177fd962ac6SDevin Teske	menu_choice=$( eval $DIALOG \
178d3a0f918SDevin Teske		--title \"\$DIALOG_TITLE\"         \
179641a6cfbSDevin Teske		--backtitle \"\$DIALOG_BACKTITLE\" \
180641a6cfbSDevin Teske		--hline \"\$hline\"                \
181641a6cfbSDevin Teske		--ok-label \"\$msg_ok\"            \
182641a6cfbSDevin Teske		--cancel-label \"\$msg_cancel\"    \
183641a6cfbSDevin Teske		--help-button                      \
184641a6cfbSDevin Teske		--help-label \"\$msg_details\"     \
185641a6cfbSDevin Teske		${SHOW_DESC:+--item-help}          \
18677599778SDevin Teske		--default-item \"\$defaultitem\"   \
18774036c4dSDevin Teske		--menu \"\$prompt\"                \
18874036c4dSDevin Teske		$height $width $rows               \
189052f8969SDevin Teske		$menu_list                         \
19089498fdfSDevin Teske		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
19189498fdfSDevin Teske	)
19289498fdfSDevin Teske	local retval=$?
193fd962ac6SDevin Teske	f_dialog_data_sanitize menu_choice
194fd962ac6SDevin Teske	f_dialog_menutag_store "$menu_choice"
195d3a0f918SDevin Teske
196d3a0f918SDevin Teske	# Only update default-item on success
197f677a9e2SDevin Teske	[ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"
198d3a0f918SDevin Teske
19989498fdfSDevin Teske	return $retval
200641a6cfbSDevin Teske}
201641a6cfbSDevin Teske
202641a6cfbSDevin Teske############################################################ MAIN
203641a6cfbSDevin Teske
204641a6cfbSDevin Teske# Incorporate rc-file if it exists
205641a6cfbSDevin Teske[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
206641a6cfbSDevin Teske
207641a6cfbSDevin Teske#
208641a6cfbSDevin Teske# Process command-line arguments
209641a6cfbSDevin Teske#
210c3755aa3SDevin Teskewhile getopts h$GETOPTS_STDARGS flag; do
211641a6cfbSDevin Teske	case "$flag" in
212641a6cfbSDevin Teske	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;
213641a6cfbSDevin Teske	esac
214641a6cfbSDevin Teskedone
215641a6cfbSDevin Teskeshift $(( $OPTIND - 1 ))
216641a6cfbSDevin Teske
217641a6cfbSDevin Teske#
218641a6cfbSDevin Teske# Initialize
219641a6cfbSDevin Teske#
220641a6cfbSDevin Teskef_dialog_title "$msg_view_edit_startup_configuration"
221641a6cfbSDevin Teskef_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
222641a6cfbSDevin Teskef_mustberoot_init
223641a6cfbSDevin Teske
2247cdf48fcSDevin Teske#
2257cdf48fcSDevin Teske# Launch application main menu
2267cdf48fcSDevin Teske#
227641a6cfbSDevin Teskewhile :; do
228641a6cfbSDevin Teske	dialog_menu_main
229641a6cfbSDevin Teske	retval=$?
230fd962ac6SDevin Teske	f_dialog_menutag_fetch mtag
231641a6cfbSDevin Teske
232641a6cfbSDevin Teske	if [ "$USE_XDIALOG" ]; then
233641a6cfbSDevin Teske		case "$mtag" in
234641a6cfbSDevin Teske		"> $msg_view_details")
235641a6cfbSDevin Teske			f_dialog_input_view_details
236641a6cfbSDevin Teske			continue
237641a6cfbSDevin Teske		esac
238f677a9e2SDevin Teske	elif [ $retval -eq $DIALOG_HELP ]; then
239641a6cfbSDevin Teske		# The ``Help'' button (labeled "Details") was pressed
240641a6cfbSDevin Teske		f_dialog_input_view_details
241641a6cfbSDevin Teske		continue
242641a6cfbSDevin Teske	fi
243641a6cfbSDevin Teske
244f677a9e2SDevin Teske	[ $retval -eq $DIALOG_OK ] || f_die
245641a6cfbSDevin Teske
246641a6cfbSDevin Teske	case "$mtag" in
247641a6cfbSDevin Teske	"X $msg_exit") break ;;
248f4a3220cSDevin Teske	"> $msg_add_new") $BSDCFG_LIBE/$APP_DIR/rcadd ${USE_XDIALOG:+-X} ;;
249641a6cfbSDevin Teske	"> $msg_delete")
250641a6cfbSDevin Teske		# rcdelete has a similar interface that can inherit the below:
251641a6cfbSDevin Teske		export SHOW_VALUE SHOW_DESC SHOW_DEFAULT_VALUE SHOW_CONFIGURED
252641a6cfbSDevin Teske		$BSDCFG_LIBE/$APP_DIR/rcdelete ${USE_XDIALOG:+-X}
253641a6cfbSDevin Teske		;;
254641a6cfbSDevin Teske	*) # Anything else is a variable to edit
255641a6cfbSDevin Teske		$BSDCFG_LIBE/$APP_DIR/rcedit ${USE_XDIALOG:+-X} "${mtag# }"
256641a6cfbSDevin Teske	esac
257641a6cfbSDevin Teskedone
258641a6cfbSDevin Teske
259641a6cfbSDevin Teskeexit $SUCCESS
260641a6cfbSDevin Teske
261641a6cfbSDevin Teske################################################################################
262641a6cfbSDevin Teske# END
263641a6cfbSDevin Teske################################################################################
264