xref: /freebsd/usr.sbin/bsdconfig/startup/rcconf (revision 775997786b786b7738ea231a6580e7ceacb4f8e0)
1641a6cfbSDevin Teske#!/bin/sh
2641a6cfbSDevin Teske#-
3e14ddd1fSDevin Teske# Copyright (c) 2012-2013 Devin Teske
4641a6cfbSDevin 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
16641a6cfbSDevin 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
20641a6cfbSDevin Teske# DAMAGES (INLUDING, 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# $FreeBSD$
28641a6cfbSDevin Teske#
29641a6cfbSDevin Teske############################################################ INCLUDES
30641a6cfbSDevin Teske
31ab2043b8SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
32ab2043b8SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
3356961fd7SDevin Teskef_dprintf "%s: loading includes..." "$0"
34ab2043b8SDevin Teskef_include $BSDCFG_SHARE/dialog.subr
35ab2043b8SDevin Teskef_include $BSDCFG_SHARE/mustberoot.subr
36ab2043b8SDevin Teskef_include $BSDCFG_SHARE/sysrc.subr
37ab2043b8SDevin Teskef_include $BSDCFG_SHARE/startup/rcconf.subr
38641a6cfbSDevin Teske
39ab2043b8SDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
40641a6cfbSDevin Teskef_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
41641a6cfbSDevin Teske
4248c5129fSDevin Teskeipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
43641a6cfbSDevin Teske[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
44641a6cfbSDevin Teske
45641a6cfbSDevin Teske############################################################ GLOBALS
46641a6cfbSDevin Teske
47641a6cfbSDevin Teske#
48641a6cfbSDevin Teske# Global map/menu-list for the main menu
49641a6cfbSDevin Teske#
50641a6cfbSDevin TeskeRCCONF_MAP=
51641a6cfbSDevin Teske_RCCONF_MAP=
52641a6cfbSDevin TeskeRCCONF_MENU_LIST=
53641a6cfbSDevin Teske
54641a6cfbSDevin Teske#
55641a6cfbSDevin Teske# Options
56641a6cfbSDevin Teske#
57641a6cfbSDevin Teske# Inherit SHOW_DESC value if set, otherwise default to 1
588d8d314dSDevin Teske[ "${SHOW_DESC+set}" ] || SHOW_DESC=1
59641a6cfbSDevin Teske# Selectively inherit SHOW_* value (in order of preference)
60641a6cfbSDevin Teskeif [ "$SHOW_DEFAULT_VALUE" ]; then
61641a6cfbSDevin Teske	SHOW_DEFAULT_VALUE=1
62641a6cfbSDevin Teske	SHOW_CONFIGURED=
63641a6cfbSDevin Teske	SHOW_VALUE=
64641a6cfbSDevin Teskeelif [ "$SHOW_CONFIGURED" ]; then
65641a6cfbSDevin Teske	SHOW_DEFAULT_VALUE=
66641a6cfbSDevin Teske	SHOW_CONFIGURED=1
67641a6cfbSDevin Teske	SHOW_VALUE=
68641a6cfbSDevin Teskeelse
69641a6cfbSDevin Teske	SHOW_DEFAULT_VALUE=
70641a6cfbSDevin Teske	SHOW_CONFIGURED=
71641a6cfbSDevin Teske	SHOW_VALUE=1
72641a6cfbSDevin Teskefi
73641a6cfbSDevin Teske
74641a6cfbSDevin Teske############################################################ FUNCTIONS
75641a6cfbSDevin Teske
76641a6cfbSDevin Teske# dialog_menu_main
77641a6cfbSDevin Teske#
78641a6cfbSDevin Teske# Display the dialog(1)-based application main menu.
79641a6cfbSDevin Teske#
80641a6cfbSDevin Teskedialog_menu_main()
81641a6cfbSDevin Teske{
82641a6cfbSDevin Teske	local hline="$hline_arrows_tab_enter"
83641a6cfbSDevin Teske	local prompt=""
84*77599778SDevin Teske	local defaultitem= # Calculated below
85641a6cfbSDevin Teske
86641a6cfbSDevin Teske	RCCONF_MENU_LIST="
87641a6cfbSDevin Teske		'X $msg_exit'    '$msg_exit_desc'
88641a6cfbSDevin Teske		                  ${SHOW_DESC:+'$msg_exit_help'}
89641a6cfbSDevin Teske		'> $msg_add_new' '$msg_add_new_desc'
90641a6cfbSDevin Teske		                  ${SHOW_DESC:+'$msg_add_new_help'}
91641a6cfbSDevin Teske		'> $msg_delete'  '$msg_delete_desc'
92641a6cfbSDevin Teske		                  ${SHOW_DESC:+'$msg_delete_help'}
93641a6cfbSDevin Teske	${USE_XDIALOG:+
94641a6cfbSDevin Teske		'> $msg_view_details' '$msg_view_details_desc'
95641a6cfbSDevin Teske		                       ${SHOW_DESC:+'$msg_view_details_help'}
96641a6cfbSDevin Teske	}
97641a6cfbSDevin Teske	" # END-QUOTE
98641a6cfbSDevin Teske
99641a6cfbSDevin Teske	if [ ! "$_RCCONF_MAP" ]; then
100641a6cfbSDevin Teske		# Genreate RCCONF_MAP of `var desc ...' per-line
101641a6cfbSDevin Teske		f_dialog_info "$msg_creating_rcconf_map"
102d3a0f918SDevin Teske		f_startup_rcconf_map RCCONF_MAP
103641a6cfbSDevin Teske		export RCCONF_MAP
104641a6cfbSDevin Teske		# Generate _${var}_desc variables from $RCCONF_MAP
105d3a0f918SDevin Teske		f_startup_rcconf_map_expand RCCONF_MAP
106641a6cfbSDevin Teske		export _RCCONF_MAP=1
107641a6cfbSDevin Teske	fi
108641a6cfbSDevin Teske
109641a6cfbSDevin Teske	# Show infobox for modes that take a while to calculate/display
110641a6cfbSDevin Teske	[ "$SHOW_DEFAULT_VALUE" -o "$SHOW_CONFIGURED" ] &&
111641a6cfbSDevin Teske		f_dialog_info "$msg_creating_menu_list"
112641a6cfbSDevin Teske
113641a6cfbSDevin Teske	RCCONF_MENU_LIST="$RCCONF_MENU_LIST $(
114fb7d723eSDevin Teske		. "$RC_DEFAULTS" > /dev/null
115fb7d723eSDevin Teske		source_rc_confs > /dev/null
116641a6cfbSDevin Teske		var_list=$( f_startup_rcconf_list )
117641a6cfbSDevin Teske		for var in $var_list; do
118641a6cfbSDevin Teske			eval export $var
119641a6cfbSDevin Teske			[ "$SHOW_DEFAULT_VALUE" ] && export \
120641a6cfbSDevin Teske				_${var}_default="$( f_sysrc_get_default $var )"
121641a6cfbSDevin Teske			[ "$SHOW_CONFIGURED" ] && export \
122641a6cfbSDevin Teske				_${var}_file="$( f_sysrc_find $var )"
123641a6cfbSDevin Teske		done
124641a6cfbSDevin Teske		export SHOW_VALUE SHOW_DESC SHOW_DEFAULT_VALUE SHOW_CONFIGURED
125641a6cfbSDevin Teske		export msg_default_value
126641a6cfbSDevin Teske		echo "$var_list" | awk '
127641a6cfbSDevin Teske		BEGIN {
128641a6cfbSDevin Teske			prefix = ""
129641a6cfbSDevin Teske		}
130641a6cfbSDevin Teske		{
131641a6cfbSDevin Teske			cur_prefix = tolower(substr($1, 1, 1))
132641a6cfbSDevin Teske			printf "'\''"
133641a6cfbSDevin Teske			if ( prefix != cur_prefix )
134641a6cfbSDevin Teske				prefix = cur_prefix
135641a6cfbSDevin Teske			else
136641a6cfbSDevin Teske				printf " "
137641a6cfbSDevin Teske			var = $1
138641a6cfbSDevin Teske			printf "%s'\'' '\''", var
139641a6cfbSDevin Teske			if ( ENVIRON["SHOW_DEFAULT_VALUE"] ) {
140641a6cfbSDevin Teske				default = ENVIRON["_" var "_default"]
141641a6cfbSDevin Teske				gsub(/'\''/, "'\''\\'\'\''", default)
142641a6cfbSDevin Teske				value = ENVIRON[var]
143641a6cfbSDevin Teske				gsub(/'\''/, "'\''\\'\'\''", value)
144641a6cfbSDevin Teske				printf ENVIRON["msg_default_value"] "; %s",
145641a6cfbSDevin Teske				       default, value
146641a6cfbSDevin Teske			} else if ( ENVIRON["SHOW_CONFIGURED"] ) {
147641a6cfbSDevin Teske				printf "%s", ENVIRON["_" var "_file"]
148641a6cfbSDevin Teske			} else { # SHOW_VALUE (default behavior)
149641a6cfbSDevin Teske				value = ENVIRON[var]
150641a6cfbSDevin Teske				gsub(/'\''/, "'\''\\'\'\''", value)
151641a6cfbSDevin Teske				printf "%s", value
152641a6cfbSDevin Teske			}
153641a6cfbSDevin Teske			printf "'\''"
154641a6cfbSDevin Teske			if ( ENVIRON["SHOW_DESC"] ) {
155641a6cfbSDevin Teske				desc = ENVIRON["_" var "_desc"]
156641a6cfbSDevin Teske				gsub(/'\''/, "'\''\\'\'\''", desc)
157641a6cfbSDevin Teske				printf " '\''%s'\''", desc
158641a6cfbSDevin Teske			}
159641a6cfbSDevin Teske			printf "\n"
160641a6cfbSDevin Teske		}'
161641a6cfbSDevin Teske	)"
162641a6cfbSDevin Teske
163641a6cfbSDevin Teske	set -f # noglob
164641a6cfbSDevin Teske
16574036c4dSDevin Teske	local height width rows
16674036c4dSDevin Teske	eval f_dialog_menu${SHOW_DESC:+_with_help}_size \
16774036c4dSDevin Teske		height width rows      \
168641a6cfbSDevin Teske		\"\$DIALOG_TITLE\"     \
169641a6cfbSDevin Teske		\"\$DIALOG_BACKTITLE\" \
170641a6cfbSDevin Teske		\"\$prompt\"           \
171641a6cfbSDevin Teske		\"\$hline\"            \
17274036c4dSDevin Teske		$RCCONF_MENU_LIST
173641a6cfbSDevin Teske
174*77599778SDevin Teske	# Obtain default-item from previously stored selection
175*77599778SDevin Teske	f_dialog_default_fetch defaultitem
176*77599778SDevin Teske
177fd962ac6SDevin Teske	local menu_choice
178fd962ac6SDevin Teske	menu_choice=$( eval $DIALOG \
179d3a0f918SDevin Teske		--title \"\$DIALOG_TITLE\"         \
180641a6cfbSDevin Teske		--backtitle \"\$DIALOG_BACKTITLE\" \
181641a6cfbSDevin Teske		--hline \"\$hline\"                \
182641a6cfbSDevin Teske		--ok-label \"\$msg_ok\"            \
183641a6cfbSDevin Teske		--cancel-label \"\$msg_cancel\"    \
184641a6cfbSDevin Teske		--help-button                      \
185641a6cfbSDevin Teske		--help-label \"\$msg_details\"     \
186641a6cfbSDevin Teske		${SHOW_DESC:+--item-help}          \
187*77599778SDevin Teske		--default-item \"\$defaultitem\"   \
18874036c4dSDevin Teske		--menu \"\$prompt\"                \
18974036c4dSDevin Teske		$height $width $rows               \
190641a6cfbSDevin Teske		$RCCONF_MENU_LIST                  \
19189498fdfSDevin Teske		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
19289498fdfSDevin Teske	)
19389498fdfSDevin Teske	local retval=$?
194fd962ac6SDevin Teske	f_dialog_data_sanitize menu_choice
195fd962ac6SDevin Teske	f_dialog_menutag_store "$menu_choice"
196d3a0f918SDevin Teske
197d3a0f918SDevin Teske	# Only update default-item on success
198*77599778SDevin Teske	[ $retval -eq 0 ] && f_dialog_default_store "$menu_choice"
199d3a0f918SDevin Teske
20089498fdfSDevin Teske	return $retval
201641a6cfbSDevin Teske}
202641a6cfbSDevin Teske
203641a6cfbSDevin Teske############################################################ MAIN
204641a6cfbSDevin Teske
205641a6cfbSDevin Teske# Incorporate rc-file if it exists
206641a6cfbSDevin Teske[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
207641a6cfbSDevin Teske
208641a6cfbSDevin Teske#
209641a6cfbSDevin Teske# Process command-line arguments
210641a6cfbSDevin Teske#
211c3755aa3SDevin Teskewhile getopts h$GETOPTS_STDARGS flag; do
212641a6cfbSDevin Teske	case "$flag" in
213641a6cfbSDevin Teske	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
214641a6cfbSDevin Teske	esac
215641a6cfbSDevin Teskedone
216641a6cfbSDevin Teskeshift $(( $OPTIND - 1 ))
217641a6cfbSDevin Teske
218641a6cfbSDevin Teske#
219641a6cfbSDevin Teske# Initialize
220641a6cfbSDevin Teske#
221641a6cfbSDevin Teskef_dialog_title "$msg_view_edit_startup_configuration"
222641a6cfbSDevin Teskef_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
223641a6cfbSDevin Teskef_mustberoot_init
224641a6cfbSDevin Teske
225641a6cfbSDevin Teskewhile :; do
226641a6cfbSDevin Teske	dialog_menu_main
227641a6cfbSDevin Teske	retval=$?
228fd962ac6SDevin Teske	f_dialog_menutag_fetch mtag
229641a6cfbSDevin Teske
230641a6cfbSDevin Teske	if [ "$USE_XDIALOG" ]; then
231641a6cfbSDevin Teske		case "$mtag" in
232641a6cfbSDevin Teske		"> $msg_view_details")
233641a6cfbSDevin Teske			f_dialog_input_view_details
234641a6cfbSDevin Teske			continue
235641a6cfbSDevin Teske		esac
236641a6cfbSDevin Teske	elif [ $retval -eq 2 ]; then
237641a6cfbSDevin Teske		# The ``Help'' button (labeled "Details") was pressed
238641a6cfbSDevin Teske		f_dialog_input_view_details
239641a6cfbSDevin Teske		continue
240641a6cfbSDevin Teske	fi
241641a6cfbSDevin Teske
242641a6cfbSDevin Teske	[ $retval -eq 0 ] || f_die
243641a6cfbSDevin Teske
244641a6cfbSDevin Teske	case "$mtag" in
245641a6cfbSDevin Teske	"X $msg_exit") break ;;
246641a6cfbSDevin Teske	"> $msg_add_new")
247641a6cfbSDevin Teske		$BSDCFG_LIBE/$APP_DIR/rcadd ${USE_XDIALOG:+-X}
248641a6cfbSDevin Teske		;;
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