xref: /freebsd/usr.sbin/bsdconfig/startup/share/rcedit.subr (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1ab2043b8SDevin Teskeif [ ! "$_STARTUP_RCEDIT_SUBR" ]; then _STARTUP_RCEDIT_SUBR=1
2ab2043b8SDevin Teske#
3ab2043b8SDevin Teske# Copyright (c) 2012 Devin Teske
4ab2043b8SDevin Teske# All rights reserved.
5ab2043b8SDevin Teske#
6ab2043b8SDevin Teske# Redistribution and use in source and binary forms, with or without
7ab2043b8SDevin Teske# modification, are permitted provided that the following conditions
8ab2043b8SDevin Teske# are met:
9ab2043b8SDevin Teske# 1. Redistributions of source code must retain the above copyright
10ab2043b8SDevin Teske#    notice, this list of conditions and the following disclaimer.
11ab2043b8SDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
12ab2043b8SDevin Teske#    notice, this list of conditions and the following disclaimer in the
13ab2043b8SDevin Teske#    documentation and/or other materials provided with the distribution.
14ab2043b8SDevin Teske#
15ab2043b8SDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16ab2043b8SDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17ab2043b8SDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ab2043b8SDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19ab2043b8SDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20ab2043b8SDevin Teske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21ab2043b8SDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22ab2043b8SDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23ab2043b8SDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24ab2043b8SDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25ab2043b8SDevin Teske# SUCH DAMAGE.
26ab2043b8SDevin Teske#
27ab2043b8SDevin Teske#
28ab2043b8SDevin Teske############################################################ INCLUDES
29ab2043b8SDevin Teske
30ab2043b8SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
31ab2043b8SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
3256961fd7SDevin Teskef_dprintf "%s: loading includes..." startup/rcedit.subr
33ab2043b8SDevin Teskef_include $BSDCFG_SHARE/dialog.subr
34*d4ae33f0SDevin Teskef_include $BSDCFG_SHARE/strings.subr
35ab2043b8SDevin Teskef_include $BSDCFG_SHARE/sysrc.subr
36ab2043b8SDevin Teske
37ab2043b8SDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
38ab2043b8SDevin Teskef_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
39ab2043b8SDevin Teske
40ab2043b8SDevin Teske############################################################ FUNCTIONS
41ab2043b8SDevin Teske
42ab2043b8SDevin Teske# f_dialog_rcedit $var [[--] $init ...]
43ab2043b8SDevin Teske#
44ab2043b8SDevin Teske# Allow the user to enter a new value for a given rc.conf(5) variable. If the
45ab2043b8SDevin Teske# user does not cancel or press ESC, the variable will be saved without
46ab2043b8SDevin Teske# confirmation.
47ab2043b8SDevin Teske#
48ab2043b8SDevin Teske# If the second argument is non-NULL, it will be processed as the initial text
49ab2043b8SDevin Teske# to be displayed, overriding the default behavior to display the currently
50ab2043b8SDevin Teske# configured value as the initial text.
51ab2043b8SDevin Teske#
52ab2043b8SDevin Teske# If instead the second argument is "--", then the third argument (NULL or
53ab2043b8SDevin Teske# otherwise) will be treated as the initial text.
54ab2043b8SDevin Teske#
55ab2043b8SDevin Teskef_dialog_rcedit()
56ab2043b8SDevin Teske{
57*d4ae33f0SDevin Teske	local funcname=f_dialog_rcedit
58298cf604SDevin Teske	local msg var="$1" _input
59ab2043b8SDevin Teske
60*d4ae33f0SDevin Teske	f_sprintf msg "$msg_please_enter_a_new_value" \
61*d4ae33f0SDevin Teske	              "$var" "$( f_sysrc_get_default "$var" )"
62ab2043b8SDevin Teske
63ab2043b8SDevin Teske	shift 1 # var
64ab2043b8SDevin Teske	if [ "$1" ]; then
65ab2043b8SDevin Teske		[ "$1" = "--" ] && shift 1 # --
66ab2043b8SDevin Teske		_input="$1"
67ab2043b8SDevin Teske	else
68ab2043b8SDevin Teske		_input=$( f_sysrc_get "$var" )
69ab2043b8SDevin Teske	fi
70ab2043b8SDevin Teske
71ab2043b8SDevin Teske	# Return if user has either pressed ESC or chosen Cancel/No
72ec7120b5SDevin Teske	f_dialog_input _input "$msg" "$_input" \
73f677a9e2SDevin Teske	               "$hline_alnum_punc_tab_enter" || return $?
74ab2043b8SDevin Teske
75ab2043b8SDevin Teske	# Return if the value has not changed from current
76ab2043b8SDevin Teske	local cur_val="$( f_sysrc_get "$var" )"
77f677a9e2SDevin Teske	[ "$_input" = "$cur_val" ] && return $DIALOG_OK
78ab2043b8SDevin Teske
79526e1dc1SDevin Teske	f_dprintf "%s: [%s]->[%s]" "$var" "$cur_val" "$_input"
80ab2043b8SDevin Teske
81*d4ae33f0SDevin Teske	f_eval_catch $funcname f_sysrc_set \
82*d4ae33f0SDevin Teske		'f_sysrc_set "%s" "%s"' "$var" "$_input"
83ab2043b8SDevin Teske}
84ab2043b8SDevin Teske
8556961fd7SDevin Teske############################################################ MAIN
8656961fd7SDevin Teske
8756961fd7SDevin Teskef_dprintf "%s: Successfully loaded." startup/rcedit.subr
8856961fd7SDevin Teske
89ab2043b8SDevin Teskefi # ! $_STARTUP_RCEDIT_SUBR
90