xref: /freebsd/usr.sbin/bsdconfig/startup/rcconf (revision a90b9d0159070121c221b966469c3e36d912bf82)
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=
50_RCCONF_MAP=
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_menu_main
75#
76# Display the dialog(1)-based application main menu.
77#
78dialog_menu_main()
79{
80	local prompt=
81	local menu_list="
82		'X $msg_exit'    '$msg_exit_desc'
83		                  ${SHOW_DESC:+'$msg_exit_this_menu'}
84		'> $msg_add_new' '$msg_add_new_desc'
85		                  ${SHOW_DESC:+'$msg_add_new_help'}
86		'> $msg_delete'  '$msg_delete_desc'
87		                  ${SHOW_DESC:+'$msg_delete_help'}
88	${USE_XDIALOG:+
89		'> $msg_view_details' '$msg_view_details_desc'
90		                       ${SHOW_DESC:+'$msg_view_details_help'}
91	}
92	" # END-QUOTE
93	local defaultitem= # Calculated below
94	local hline="$hline_arrows_tab_enter"
95
96	if [ ! "$_RCCONF_MAP" ]; then
97		# Genreate RCCONF_MAP of `var desc ...' per-line
98		f_dialog_info "$msg_creating_rcconf_map"
99		f_startup_rcconf_map RCCONF_MAP
100		export RCCONF_MAP
101		# Generate _${var}_desc variables from $RCCONF_MAP
102		f_startup_rcconf_map_expand RCCONF_MAP
103		export _RCCONF_MAP=1
104	fi
105
106	# Show infobox for modes that take a while to calculate/display
107	[ "$SHOW_DEFAULT_VALUE" -o "$SHOW_CONFIGURED" ] &&
108		f_dialog_info "$msg_creating_menu_list"
109
110	menu_list="$menu_list $(
111		. "$RC_DEFAULTS" > /dev/null
112		source_rc_confs > /dev/null
113		var_list=$( f_startup_rcconf_list )
114		for var in $var_list; do
115			eval export $var
116			[ "$SHOW_DEFAULT_VALUE" ] && export \
117				_${var}_default="$( f_sysrc_get_default $var )"
118			[ "$SHOW_CONFIGURED" ] && export \
119				_${var}_file="$( f_sysrc_find $var )"
120		done
121		export SHOW_VALUE SHOW_DESC SHOW_DEFAULT_VALUE SHOW_CONFIGURED
122		export msg_default_value
123		echo "$var_list" | awk '
124		BEGIN {
125			prefix = ""
126		}
127		{
128			cur_prefix = tolower(substr($1, 1, 1))
129			printf "'\''"
130			if ( prefix != cur_prefix )
131				prefix = cur_prefix
132			else
133				printf " "
134			var = $1
135			printf "%s'\'' '\''", var
136			if ( ENVIRON["SHOW_DEFAULT_VALUE"] ) {
137				default = ENVIRON["_" var "_default"]
138				gsub(/'\''/, "'\''\\'\'\''", default)
139				value = ENVIRON[var]
140				gsub(/'\''/, "'\''\\'\'\''", value)
141				printf ENVIRON["msg_default_value"] "; %s",
142				       default, value
143			} else if ( ENVIRON["SHOW_CONFIGURED"] ) {
144				printf "%s", ENVIRON["_" var "_file"]
145			} else { # SHOW_VALUE (default behavior)
146				value = ENVIRON[var]
147				gsub(/'\''/, "'\''\\'\'\''", value)
148				printf "%s", value
149			}
150			printf "'\''"
151			if ( ENVIRON["SHOW_DESC"] ) {
152				desc = ENVIRON["_" var "_desc"]
153				gsub(/'\''/, "'\''\\'\'\''", desc)
154				printf " '\''%s'\''", desc
155			}
156			printf "\n"
157		}'
158	)"
159
160	set -f # set noglob because descriptions in the $menu_list may contain
161	       # `*' and get expanded by dialog(1) (doesn't affect Xdialog(1)).
162	       # This prevents dialog(1) from expanding wildcards in help line.
163
164	local height width rows
165	eval f_dialog_menu${SHOW_DESC:+_with_help}_size \
166		height width rows      \
167		\"\$DIALOG_TITLE\"     \
168		\"\$DIALOG_BACKTITLE\" \
169		\"\$prompt\"           \
170		\"\$hline\"            \
171		$menu_list
172
173	# Obtain default-item from previously stored selection
174	f_dialog_default_fetch defaultitem
175
176	local menu_choice
177	menu_choice=$( eval $DIALOG \
178		--title \"\$DIALOG_TITLE\"         \
179		--backtitle \"\$DIALOG_BACKTITLE\" \
180		--hline \"\$hline\"                \
181		--ok-label \"\$msg_ok\"            \
182		--cancel-label \"\$msg_cancel\"    \
183		--help-button                      \
184		--help-label \"\$msg_details\"     \
185		${SHOW_DESC:+--item-help}          \
186		--default-item \"\$defaultitem\"   \
187		--menu \"\$prompt\"                \
188		$height $width $rows               \
189		$menu_list                         \
190		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
191	)
192	local retval=$?
193	f_dialog_data_sanitize menu_choice
194	f_dialog_menutag_store "$menu_choice"
195
196	# Only update default-item on success
197	[ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"
198
199	return $retval
200}
201
202############################################################ MAIN
203
204# Incorporate rc-file if it exists
205[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
206
207#
208# Process command-line arguments
209#
210while getopts h$GETOPTS_STDARGS flag; do
211	case "$flag" in
212	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;
213	esac
214done
215shift $(( $OPTIND - 1 ))
216
217#
218# Initialize
219#
220f_dialog_title "$msg_view_edit_startup_configuration"
221f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
222f_mustberoot_init
223
224#
225# Launch application main menu
226#
227while :; do
228	dialog_menu_main
229	retval=$?
230	f_dialog_menutag_fetch mtag
231
232	if [ "$USE_XDIALOG" ]; then
233		case "$mtag" in
234		"> $msg_view_details")
235			f_dialog_input_view_details
236			continue
237		esac
238	elif [ $retval -eq $DIALOG_HELP ]; then
239		# The ``Help'' button (labeled "Details") was pressed
240		f_dialog_input_view_details
241		continue
242	fi
243
244	[ $retval -eq $DIALOG_OK ] || f_die
245
246	case "$mtag" in
247	"X $msg_exit") break ;;
248	"> $msg_add_new") $BSDCFG_LIBE/$APP_DIR/rcadd ${USE_XDIALOG:+-X} ;;
249	"> $msg_delete")
250		# rcdelete has a similar interface that can inherit the below:
251		export SHOW_VALUE SHOW_DESC SHOW_DEFAULT_VALUE SHOW_CONFIGURED
252		$BSDCFG_LIBE/$APP_DIR/rcdelete ${USE_XDIALOG:+-X}
253		;;
254	*) # Anything else is a variable to edit
255		$BSDCFG_LIBE/$APP_DIR/rcedit ${USE_XDIALOG:+-X} "${mtag# }"
256	esac
257done
258
259exit $SUCCESS
260
261################################################################################
262# END
263################################################################################
264