rcconf revision 244675
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: head/usr.sbin/bsdconfig/startup/rcconf 244675 2012-12-25 10:47:45Z dteske $
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." "$0"
34f_include $BSDCFG_SHARE/dialog.subr
35f_include $BSDCFG_SHARE/mustberoot.subr
36f_include $BSDCFG_SHARE/sysrc.subr
37f_include $BSDCFG_SHARE/startup/rcconf.subr
38
39BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
40f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
41
42ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
43[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
44
45############################################################ GLOBALS
46
47#
48# Global map/menu-list for the main menu
49#
50RCCONF_MAP=
51_RCCONF_MAP=
52RCCONF_MENU_LIST=
53
54#
55# Options
56#
57# Inherit SHOW_DESC value if set, otherwise default to 1
58[ "${SHOW_DESC+set}" ] || SHOW_DESC=1
59# Selectively inherit SHOW_* value (in order of preference)
60if [ "$SHOW_DEFAULT_VALUE" ]; then
61	SHOW_DEFAULT_VALUE=1
62	SHOW_CONFIGURED=
63	SHOW_VALUE=
64elif [ "$SHOW_CONFIGURED" ]; then
65	SHOW_DEFAULT_VALUE=
66	SHOW_CONFIGURED=1
67	SHOW_VALUE=
68else
69	SHOW_DEFAULT_VALUE=
70	SHOW_CONFIGURED=
71	SHOW_VALUE=1
72fi
73
74############################################################ FUNCTIONS
75
76# dialog_menu_main
77#
78# Display the dialog(1)-based application main menu.
79#
80dialog_menu_main()
81{
82	local size
83	local hline="$hline_arrows_tab_enter"
84	local prompt=""
85
86	RCCONF_MENU_LIST="
87		'X $msg_exit'    '$msg_exit_desc'
88		                  ${SHOW_DESC:+'$msg_exit_help'}
89		'> $msg_add_new' '$msg_add_new_desc'
90		                  ${SHOW_DESC:+'$msg_add_new_help'}
91		'> $msg_delete'  '$msg_delete_desc'
92		                  ${SHOW_DESC:+'$msg_delete_help'}
93	${USE_XDIALOG:+
94		'> $msg_view_details' '$msg_view_details_desc'
95		                       ${SHOW_DESC:+'$msg_view_details_help'}
96	}
97	" # END-QUOTE
98
99	if [ ! "$_RCCONF_MAP" ]; then
100		# Genreate RCCONF_MAP of `var desc ...' per-line
101		f_dialog_info "$msg_creating_rcconf_map"
102		RCCONF_MAP=$( f_startup_rcconf_map )
103		export RCCONF_MAP
104		# Generate _${var}_desc variables from $RCCONF_MAP
105		f_startup_rcconf_map_expand
106		export _RCCONF_MAP=1
107	fi
108
109	# Show infobox for modes that take a while to calculate/display
110	[ "$SHOW_DEFAULT_VALUE" -o "$SHOW_CONFIGURED" ] &&
111		f_dialog_info "$msg_creating_menu_list"
112
113	RCCONF_MENU_LIST="$RCCONF_MENU_LIST $(
114		. "$RC_DEFAULTS" > /dev/null
115		source_rc_confs > /dev/null
116		var_list=$( f_startup_rcconf_list )
117		for var in $var_list; do
118			eval export $var
119			[ "$SHOW_DEFAULT_VALUE" ] && export \
120				_${var}_default="$( f_sysrc_get_default $var )"
121			[ "$SHOW_CONFIGURED" ] && export \
122				_${var}_file="$( f_sysrc_find $var )"
123		done
124		export SHOW_VALUE SHOW_DESC SHOW_DEFAULT_VALUE SHOW_CONFIGURED
125		export msg_default_value
126		echo "$var_list" | awk '
127		BEGIN {
128			prefix = ""
129		}
130		{
131			cur_prefix = tolower(substr($1, 1, 1))
132			printf "'\''"
133			if ( prefix != cur_prefix )
134				prefix = cur_prefix
135			else
136				printf " "
137			var = $1
138			printf "%s'\'' '\''", var
139			if ( ENVIRON["SHOW_DEFAULT_VALUE"] ) {
140				default = ENVIRON["_" var "_default"]
141				gsub(/'\''/, "'\''\\'\'\''", default)
142				value = ENVIRON[var]
143				gsub(/'\''/, "'\''\\'\'\''", value)
144				printf ENVIRON["msg_default_value"] "; %s",
145				       default, value
146			} else if ( ENVIRON["SHOW_CONFIGURED"] ) {
147				printf "%s", ENVIRON["_" var "_file"]
148			} else { # SHOW_VALUE (default behavior)
149				value = ENVIRON[var]
150				gsub(/'\''/, "'\''\\'\'\''", value)
151				printf "%s", value
152			}
153			printf "'\''"
154			if ( ENVIRON["SHOW_DESC"] ) {
155				desc = ENVIRON["_" var "_desc"]
156				gsub(/'\''/, "'\''\\'\'\''", desc)
157				printf " '\''%s'\''", desc
158			}
159			printf "\n"
160		}'
161	)"
162
163	set -f # noglob
164
165	size=$( eval f_dialog_menu_${SHOW_DESC:+with_help_}size \
166	        	\"\$DIALOG_TITLE\"     \
167	        	\"\$DIALOG_BACKTITLE\" \
168	                \"\$prompt\"           \
169	        	\"\$hline\"            \
170	        	$RCCONF_MENU_LIST      )
171
172	local dialog_menu
173	dialog_menu=$( eval $DIALOG \
174		--clear --title \"\$DIALOG_TITLE\" \
175		--backtitle \"\$DIALOG_BACKTITLE\" \
176		--hline \"\$hline\"                \
177		--ok-label \"\$msg_ok\"            \
178		--cancel-label \"\$msg_cancel\"    \
179		--help-button                      \
180		--help-label \"\$msg_details\"     \
181		${SHOW_DESC:+--item-help}          \
182		--menu \"\$prompt\" $size          \
183		$RCCONF_MENU_LIST                  \
184		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
185	)
186	local retval=$?
187	setvar DIALOG_MENU_$$ "$dialog_menu"
188	return $retval
189}
190
191############################################################ MAIN
192
193# Incorporate rc-file if it exists
194[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
195
196#
197# Process command-line arguments
198#
199while getopts hSX flag; do
200	case "$flag" in
201	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
202	esac
203done
204shift $(( $OPTIND - 1 ))
205
206#
207# Initialize
208#
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