xref: /freebsd/usr.sbin/bsdconfig/startup/rcconf (revision 89498fdf4174b08bb67d684d14886539aff698b4)
1#!/bin/sh
2#-
3# Copyright (c) 2012 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 (INLUDING, 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_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
41ipgm=$( f_index_menu_selection $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
42[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
43
44############################################################ GLOBALS
45
46#
47# Global map/menu-list for the main menu
48#
49RCCONF_MAP=
50_RCCONF_MAP=
51RCCONF_MENU_LIST=
52
53#
54# Options
55#
56# Inherit SHOW_DESC value if set, otherwise default to 1
57( : ${SHOW_DESC?} ) > /dev/null 2>&1 || 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_menu_main
76#
77# Display the dialog(1)-based application main menu.
78#
79dialog_menu_main()
80{
81	local size
82	local hline="$hline_arrows_tab_enter"
83	local prompt=""
84
85	RCCONF_MENU_LIST="
86		'X $msg_exit'    '$msg_exit_desc'
87		                  ${SHOW_DESC:+'$msg_exit_help'}
88		'> $msg_add_new' '$msg_add_new_desc'
89		                  ${SHOW_DESC:+'$msg_add_new_help'}
90		'> $msg_delete'  '$msg_delete_desc'
91		                  ${SHOW_DESC:+'$msg_delete_help'}
92	${USE_XDIALOG:+
93		'> $msg_view_details' '$msg_view_details_desc'
94		                       ${SHOW_DESC:+'$msg_view_details_help'}
95	}
96	" # END-QUOTE
97
98	if [ ! "$_RCCONF_MAP" ]; then
99		# Genreate RCCONF_MAP of `var desc ...' per-line
100		f_dialog_info "$msg_creating_rcconf_map"
101		RCCONF_MAP=$( f_startup_rcconf_map )
102		export RCCONF_MAP
103		# Generate _${var}_desc variables from $RCCONF_MAP
104		f_startup_rcconf_map_expand
105		export _RCCONF_MAP=1
106	fi
107
108	# Show infobox for modes that take a while to calculate/display
109	[ "$SHOW_DEFAULT_VALUE" -o "$SHOW_CONFIGURED" ] &&
110		f_dialog_info "$msg_creating_menu_list"
111
112	RCCONF_MENU_LIST="$RCCONF_MENU_LIST $(
113		. "$RC_DEFAULTS" > /dev/null
114		source_rc_confs > /dev/null
115		var_list=$( f_startup_rcconf_list )
116		for var in $var_list; do
117			eval export $var
118			[ "$SHOW_DEFAULT_VALUE" ] && export \
119				_${var}_default="$( f_sysrc_get_default $var )"
120			[ "$SHOW_CONFIGURED" ] && export \
121				_${var}_file="$( f_sysrc_find $var )"
122		done
123		export SHOW_VALUE SHOW_DESC SHOW_DEFAULT_VALUE SHOW_CONFIGURED
124		export msg_default_value
125		echo "$var_list" | awk '
126		BEGIN {
127			prefix = ""
128		}
129		{
130			cur_prefix = tolower(substr($1, 1, 1))
131			printf "'\''"
132			if ( prefix != cur_prefix )
133				prefix = cur_prefix
134			else
135				printf " "
136			var = $1
137			printf "%s'\'' '\''", var
138			if ( ENVIRON["SHOW_DEFAULT_VALUE"] ) {
139				default = ENVIRON["_" var "_default"]
140				gsub(/'\''/, "'\''\\'\'\''", default)
141				value = ENVIRON[var]
142				gsub(/'\''/, "'\''\\'\'\''", value)
143				printf ENVIRON["msg_default_value"] "; %s",
144				       default, value
145			} else if ( ENVIRON["SHOW_CONFIGURED"] ) {
146				printf "%s", ENVIRON["_" var "_file"]
147			} else { # SHOW_VALUE (default behavior)
148				value = ENVIRON[var]
149				gsub(/'\''/, "'\''\\'\'\''", value)
150				printf "%s", value
151			}
152			printf "'\''"
153			if ( ENVIRON["SHOW_DESC"] ) {
154				desc = ENVIRON["_" var "_desc"]
155				gsub(/'\''/, "'\''\\'\'\''", desc)
156				printf " '\''%s'\''", desc
157			}
158			printf "\n"
159		}'
160	)"
161
162	set -f # noglob
163
164	size=$( eval f_dialog_menu_${SHOW_DESC:+with_help_}size \
165	        	\"\$DIALOG_TITLE\"     \
166	        	\"\$DIALOG_BACKTITLE\" \
167	                \"\$prompt\"           \
168	        	\"\$hline\"            \
169	        	$RCCONF_MENU_LIST      )
170
171	local dialog_menu
172	dialog_menu=$( eval $DIALOG \
173		--clear --title \"\$DIALOG_TITLE\" \
174		--backtitle \"\$DIALOG_BACKTITLE\" \
175		--hline \"\$hline\"                \
176		--ok-label \"\$msg_ok\"            \
177		--cancel-label \"\$msg_cancel\"    \
178		--help-button                      \
179		--help-label \"\$msg_details\"     \
180		${SHOW_DESC:+--item-help}          \
181		--menu \"\$prompt\" $size          \
182		$RCCONF_MENU_LIST                  \
183		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
184	)
185	local retval=$?
186	setvar DIALOG_MENU_$$ "$dialog_menu"
187	return $retval
188}
189
190############################################################ MAIN
191
192# Incorporate rc-file if it exists
193[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
194
195#
196# Process command-line arguments
197#
198while getopts hSX flag; do
199	case "$flag" in
200	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
201	esac
202done
203shift $(( $OPTIND - 1 ))
204
205#
206# Initialize
207#
208f_dialog_init
209f_dialog_title "$msg_view_edit_startup_configuration"
210f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
211f_mustberoot_init
212
213while :; do
214	dialog_menu_main
215	retval=$?
216	mtag=$( f_dialog_menutag )
217
218	if [ "$USE_XDIALOG" ]; then
219		case "$mtag" in
220		"> $msg_view_details")
221			f_dialog_input_view_details
222			continue
223		esac
224	elif [ $retval -eq 2 ]; then
225		# The ``Help'' button (labeled "Details") was pressed
226		f_dialog_input_view_details
227		continue
228	fi
229
230	[ $retval -eq 0 ] || f_die
231
232	case "$mtag" in
233	"X $msg_exit") break ;;
234	"> $msg_add_new")
235		$BSDCFG_LIBE/$APP_DIR/rcadd ${USE_XDIALOG:+-X}
236		;;
237	"> $msg_delete")
238		# rcdelete has a similar interface that can inherit the below:
239		export SHOW_VALUE SHOW_DESC SHOW_DEFAULT_VALUE SHOW_CONFIGURED
240		$BSDCFG_LIBE/$APP_DIR/rcdelete ${USE_XDIALOG:+-X}
241		;;
242	*) # Anything else is a variable to edit
243		$BSDCFG_LIBE/$APP_DIR/rcedit ${USE_XDIALOG:+-X} "${mtag# }"
244	esac
245done
246
247exit $SUCCESS
248
249################################################################################
250# END
251################################################################################
252