xref: /freebsd/usr.sbin/bsdconfig/startup/rcconf (revision 74036c4de983981ee2b23f91e285363293aa7c3c)
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=""
84641a6cfbSDevin Teske
85641a6cfbSDevin Teske	RCCONF_MENU_LIST="
86641a6cfbSDevin Teske		'X $msg_exit'    '$msg_exit_desc'
87641a6cfbSDevin Teske		                  ${SHOW_DESC:+'$msg_exit_help'}
88641a6cfbSDevin Teske		'> $msg_add_new' '$msg_add_new_desc'
89641a6cfbSDevin Teske		                  ${SHOW_DESC:+'$msg_add_new_help'}
90641a6cfbSDevin Teske		'> $msg_delete'  '$msg_delete_desc'
91641a6cfbSDevin Teske		                  ${SHOW_DESC:+'$msg_delete_help'}
92641a6cfbSDevin Teske	${USE_XDIALOG:+
93641a6cfbSDevin Teske		'> $msg_view_details' '$msg_view_details_desc'
94641a6cfbSDevin Teske		                       ${SHOW_DESC:+'$msg_view_details_help'}
95641a6cfbSDevin Teske	}
96641a6cfbSDevin Teske	" # END-QUOTE
97641a6cfbSDevin Teske
98641a6cfbSDevin Teske	if [ ! "$_RCCONF_MAP" ]; then
99641a6cfbSDevin Teske		# Genreate RCCONF_MAP of `var desc ...' per-line
100641a6cfbSDevin Teske		f_dialog_info "$msg_creating_rcconf_map"
101d3a0f918SDevin Teske		f_startup_rcconf_map RCCONF_MAP
102641a6cfbSDevin Teske		export RCCONF_MAP
103641a6cfbSDevin Teske		# Generate _${var}_desc variables from $RCCONF_MAP
104d3a0f918SDevin Teske		f_startup_rcconf_map_expand RCCONF_MAP
105641a6cfbSDevin Teske		export _RCCONF_MAP=1
106641a6cfbSDevin Teske	fi
107641a6cfbSDevin Teske
108641a6cfbSDevin Teske	# Show infobox for modes that take a while to calculate/display
109641a6cfbSDevin Teske	[ "$SHOW_DEFAULT_VALUE" -o "$SHOW_CONFIGURED" ] &&
110641a6cfbSDevin Teske		f_dialog_info "$msg_creating_menu_list"
111641a6cfbSDevin Teske
112641a6cfbSDevin Teske	RCCONF_MENU_LIST="$RCCONF_MENU_LIST $(
113fb7d723eSDevin Teske		. "$RC_DEFAULTS" > /dev/null
114fb7d723eSDevin Teske		source_rc_confs > /dev/null
115641a6cfbSDevin Teske		var_list=$( f_startup_rcconf_list )
116641a6cfbSDevin Teske		for var in $var_list; do
117641a6cfbSDevin Teske			eval export $var
118641a6cfbSDevin Teske			[ "$SHOW_DEFAULT_VALUE" ] && export \
119641a6cfbSDevin Teske				_${var}_default="$( f_sysrc_get_default $var )"
120641a6cfbSDevin Teske			[ "$SHOW_CONFIGURED" ] && export \
121641a6cfbSDevin Teske				_${var}_file="$( f_sysrc_find $var )"
122641a6cfbSDevin Teske		done
123641a6cfbSDevin Teske		export SHOW_VALUE SHOW_DESC SHOW_DEFAULT_VALUE SHOW_CONFIGURED
124641a6cfbSDevin Teske		export msg_default_value
125641a6cfbSDevin Teske		echo "$var_list" | awk '
126641a6cfbSDevin Teske		BEGIN {
127641a6cfbSDevin Teske			prefix = ""
128641a6cfbSDevin Teske		}
129641a6cfbSDevin Teske		{
130641a6cfbSDevin Teske			cur_prefix = tolower(substr($1, 1, 1))
131641a6cfbSDevin Teske			printf "'\''"
132641a6cfbSDevin Teske			if ( prefix != cur_prefix )
133641a6cfbSDevin Teske				prefix = cur_prefix
134641a6cfbSDevin Teske			else
135641a6cfbSDevin Teske				printf " "
136641a6cfbSDevin Teske			var = $1
137641a6cfbSDevin Teske			printf "%s'\'' '\''", var
138641a6cfbSDevin Teske			if ( ENVIRON["SHOW_DEFAULT_VALUE"] ) {
139641a6cfbSDevin Teske				default = ENVIRON["_" var "_default"]
140641a6cfbSDevin Teske				gsub(/'\''/, "'\''\\'\'\''", default)
141641a6cfbSDevin Teske				value = ENVIRON[var]
142641a6cfbSDevin Teske				gsub(/'\''/, "'\''\\'\'\''", value)
143641a6cfbSDevin Teske				printf ENVIRON["msg_default_value"] "; %s",
144641a6cfbSDevin Teske				       default, value
145641a6cfbSDevin Teske			} else if ( ENVIRON["SHOW_CONFIGURED"] ) {
146641a6cfbSDevin Teske				printf "%s", ENVIRON["_" var "_file"]
147641a6cfbSDevin Teske			} else { # SHOW_VALUE (default behavior)
148641a6cfbSDevin Teske				value = ENVIRON[var]
149641a6cfbSDevin Teske				gsub(/'\''/, "'\''\\'\'\''", value)
150641a6cfbSDevin Teske				printf "%s", value
151641a6cfbSDevin Teske			}
152641a6cfbSDevin Teske			printf "'\''"
153641a6cfbSDevin Teske			if ( ENVIRON["SHOW_DESC"] ) {
154641a6cfbSDevin Teske				desc = ENVIRON["_" var "_desc"]
155641a6cfbSDevin Teske				gsub(/'\''/, "'\''\\'\'\''", desc)
156641a6cfbSDevin Teske				printf " '\''%s'\''", desc
157641a6cfbSDevin Teske			}
158641a6cfbSDevin Teske			printf "\n"
159641a6cfbSDevin Teske		}'
160641a6cfbSDevin Teske	)"
161641a6cfbSDevin Teske
162641a6cfbSDevin Teske	set -f # noglob
163641a6cfbSDevin Teske
164*74036c4dSDevin Teske	local height width rows
165*74036c4dSDevin Teske	eval f_dialog_menu${SHOW_DESC:+_with_help}_size \
166*74036c4dSDevin Teske		height width rows      \
167641a6cfbSDevin Teske		\"\$DIALOG_TITLE\"     \
168641a6cfbSDevin Teske		\"\$DIALOG_BACKTITLE\" \
169641a6cfbSDevin Teske		\"\$prompt\"           \
170641a6cfbSDevin Teske		\"\$hline\"            \
171*74036c4dSDevin Teske		$RCCONF_MENU_LIST
172641a6cfbSDevin Teske
17389498fdfSDevin Teske	local dialog_menu
17489498fdfSDevin Teske	dialog_menu=$( eval $DIALOG \
175d3a0f918SDevin Teske		--title \"\$DIALOG_TITLE\"          \
176641a6cfbSDevin Teske		--backtitle \"\$DIALOG_BACKTITLE\"  \
177641a6cfbSDevin Teske		--hline \"\$hline\"                 \
178641a6cfbSDevin Teske		--ok-label \"\$msg_ok\"             \
179641a6cfbSDevin Teske		--cancel-label \"\$msg_cancel\"     \
180641a6cfbSDevin Teske		--help-button                       \
181641a6cfbSDevin Teske		--help-label \"\$msg_details\"      \
182641a6cfbSDevin Teske		${SHOW_DESC:+--item-help}           \
183d3a0f918SDevin Teske		--default-item \"\$DEFAULTITEM_$$\" \
184*74036c4dSDevin Teske		--menu \"\$prompt\"                 \
185*74036c4dSDevin Teske		$height $width $rows                \
186641a6cfbSDevin Teske		$RCCONF_MENU_LIST                   \
18789498fdfSDevin Teske		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
18889498fdfSDevin Teske	)
18989498fdfSDevin Teske	local retval=$?
19089498fdfSDevin Teske	setvar DIALOG_MENU_$$ "$dialog_menu"
191d3a0f918SDevin Teske
192d3a0f918SDevin Teske	# Only update default-item on success
193d3a0f918SDevin Teske	[ $retval -eq 0 ] && setvar DEFAULTITEM_$$ "$dialog_menu"
194d3a0f918SDevin Teske
19589498fdfSDevin Teske	return $retval
196641a6cfbSDevin Teske}
197641a6cfbSDevin Teske
198641a6cfbSDevin Teske############################################################ MAIN
199641a6cfbSDevin Teske
200641a6cfbSDevin Teske# Incorporate rc-file if it exists
201641a6cfbSDevin Teske[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
202641a6cfbSDevin Teske
203641a6cfbSDevin Teske#
204641a6cfbSDevin Teske# Process command-line arguments
205641a6cfbSDevin Teske#
206c3755aa3SDevin Teskewhile getopts h$GETOPTS_STDARGS flag; do
207641a6cfbSDevin Teske	case "$flag" in
208641a6cfbSDevin Teske	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
209641a6cfbSDevin Teske	esac
210641a6cfbSDevin Teskedone
211641a6cfbSDevin Teskeshift $(( $OPTIND - 1 ))
212641a6cfbSDevin Teske
213641a6cfbSDevin Teske#
214641a6cfbSDevin Teske# Initialize
215641a6cfbSDevin Teske#
216641a6cfbSDevin Teskef_dialog_title "$msg_view_edit_startup_configuration"
217641a6cfbSDevin Teskef_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
218641a6cfbSDevin Teskef_mustberoot_init
219641a6cfbSDevin Teske
220641a6cfbSDevin Teskewhile :; do
221641a6cfbSDevin Teske	dialog_menu_main
222641a6cfbSDevin Teske	retval=$?
223641a6cfbSDevin Teske	mtag=$( f_dialog_menutag )
224641a6cfbSDevin Teske
225641a6cfbSDevin Teske	if [ "$USE_XDIALOG" ]; then
226641a6cfbSDevin Teske		case "$mtag" in
227641a6cfbSDevin Teske		"> $msg_view_details")
228641a6cfbSDevin Teske			f_dialog_input_view_details
229641a6cfbSDevin Teske			continue
230641a6cfbSDevin Teske		esac
231641a6cfbSDevin Teske	elif [ $retval -eq 2 ]; then
232641a6cfbSDevin Teske		# The ``Help'' button (labeled "Details") was pressed
233641a6cfbSDevin Teske		f_dialog_input_view_details
234641a6cfbSDevin Teske		continue
235641a6cfbSDevin Teske	fi
236641a6cfbSDevin Teske
237641a6cfbSDevin Teske	[ $retval -eq 0 ] || f_die
238641a6cfbSDevin Teske
239641a6cfbSDevin Teske	case "$mtag" in
240641a6cfbSDevin Teske	"X $msg_exit") break ;;
241641a6cfbSDevin Teske	"> $msg_add_new")
242641a6cfbSDevin Teske		$BSDCFG_LIBE/$APP_DIR/rcadd ${USE_XDIALOG:+-X}
243641a6cfbSDevin Teske		;;
244641a6cfbSDevin Teske	"> $msg_delete")
245641a6cfbSDevin Teske		# rcdelete has a similar interface that can inherit the below:
246641a6cfbSDevin Teske		export SHOW_VALUE SHOW_DESC SHOW_DEFAULT_VALUE SHOW_CONFIGURED
247641a6cfbSDevin Teske		$BSDCFG_LIBE/$APP_DIR/rcdelete ${USE_XDIALOG:+-X}
248641a6cfbSDevin Teske		;;
249641a6cfbSDevin Teske	*) # Anything else is a variable to edit
250641a6cfbSDevin Teske		$BSDCFG_LIBE/$APP_DIR/rcedit ${USE_XDIALOG:+-X} "${mtag# }"
251641a6cfbSDevin Teske	esac
252641a6cfbSDevin Teskedone
253641a6cfbSDevin Teske
254641a6cfbSDevin Teskeexit $SUCCESS
255641a6cfbSDevin Teske
256641a6cfbSDevin Teske################################################################################
257641a6cfbSDevin Teske# END
258641a6cfbSDevin Teske################################################################################
259