Deleted Added
full compact
rcconf (251190) rcconf (251236)
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 (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#
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 (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 251190 2013-05-31 19:07:17Z dteske $
27# $FreeBSD: head/usr.sbin/bsdconfig/startup/rcconf 251236 2013-06-01 23:58:44Z 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 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 f_startup_rcconf_map RCCONF_MAP
102 export RCCONF_MAP
103 # Generate _${var}_desc variables from $RCCONF_MAP
104 f_startup_rcconf_map_expand RCCONF_MAP
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 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 $RCCONF_MENU_LIST
172
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 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 f_startup_rcconf_map RCCONF_MAP
102 export RCCONF_MAP
103 # Generate _${var}_desc variables from $RCCONF_MAP
104 f_startup_rcconf_map_expand RCCONF_MAP
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 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 $RCCONF_MENU_LIST
172
173 local dialog_menu
174 dialog_menu=$( eval $DIALOG \
173 local menu_choice
174 menu_choice=$( eval $DIALOG \
175 --title \"\$DIALOG_TITLE\" \
176 --backtitle \"\$DIALOG_BACKTITLE\" \
177 --hline \"\$hline\" \
178 --ok-label \"\$msg_ok\" \
179 --cancel-label \"\$msg_cancel\" \
180 --help-button \
181 --help-label \"\$msg_details\" \
182 ${SHOW_DESC:+--item-help} \
183 --default-item \"\$DEFAULTITEM_$$\" \
184 --menu \"\$prompt\" \
185 $height $width $rows \
186 $RCCONF_MENU_LIST \
187 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
188 )
189 local retval=$?
175 --title \"\$DIALOG_TITLE\" \
176 --backtitle \"\$DIALOG_BACKTITLE\" \
177 --hline \"\$hline\" \
178 --ok-label \"\$msg_ok\" \
179 --cancel-label \"\$msg_cancel\" \
180 --help-button \
181 --help-label \"\$msg_details\" \
182 ${SHOW_DESC:+--item-help} \
183 --default-item \"\$DEFAULTITEM_$$\" \
184 --menu \"\$prompt\" \
185 $height $width $rows \
186 $RCCONF_MENU_LIST \
187 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
188 )
189 local retval=$?
190 setvar DIALOG_MENU_$$ "$dialog_menu"
190 f_dialog_data_sanitize menu_choice
191 f_dialog_menutag_store "$menu_choice"
191
192 # Only update default-item on success
192
193 # Only update default-item on success
193 [ $retval -eq 0 ] && setvar DEFAULTITEM_$$ "$dialog_menu"
194 [ $retval -eq 0 ] && setvar DEFAULTITEM_$$ "$menu_choice"
194
195 return $retval
196}
197
198############################################################ MAIN
199
200# Incorporate rc-file if it exists
201[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
202
203#
204# Process command-line arguments
205#
206while getopts h$GETOPTS_STDARGS flag; do
207 case "$flag" in
208 h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
209 esac
210done
211shift $(( $OPTIND - 1 ))
212
213#
214# Initialize
215#
216f_dialog_title "$msg_view_edit_startup_configuration"
217f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
218f_mustberoot_init
219
220while :; do
221 dialog_menu_main
222 retval=$?
195
196 return $retval
197}
198
199############################################################ MAIN
200
201# Incorporate rc-file if it exists
202[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
203
204#
205# Process command-line arguments
206#
207while getopts h$GETOPTS_STDARGS flag; do
208 case "$flag" in
209 h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
210 esac
211done
212shift $(( $OPTIND - 1 ))
213
214#
215# Initialize
216#
217f_dialog_title "$msg_view_edit_startup_configuration"
218f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
219f_mustberoot_init
220
221while :; do
222 dialog_menu_main
223 retval=$?
223 mtag=$( f_dialog_menutag )
224 f_dialog_menutag_fetch mtag
224
225 if [ "$USE_XDIALOG" ]; then
226 case "$mtag" in
227 "> $msg_view_details")
228 f_dialog_input_view_details
229 continue
230 esac
231 elif [ $retval -eq 2 ]; then
232 # The ``Help'' button (labeled "Details") was pressed
233 f_dialog_input_view_details
234 continue
235 fi
236
237 [ $retval -eq 0 ] || f_die
238
239 case "$mtag" in
240 "X $msg_exit") break ;;
241 "> $msg_add_new")
242 $BSDCFG_LIBE/$APP_DIR/rcadd ${USE_XDIALOG:+-X}
243 ;;
244 "> $msg_delete")
245 # rcdelete has a similar interface that can inherit the below:
246 export SHOW_VALUE SHOW_DESC SHOW_DEFAULT_VALUE SHOW_CONFIGURED
247 $BSDCFG_LIBE/$APP_DIR/rcdelete ${USE_XDIALOG:+-X}
248 ;;
249 *) # Anything else is a variable to edit
250 $BSDCFG_LIBE/$APP_DIR/rcedit ${USE_XDIALOG:+-X} "${mtag# }"
251 esac
252done
253
254exit $SUCCESS
255
256################################################################################
257# END
258################################################################################
225
226 if [ "$USE_XDIALOG" ]; then
227 case "$mtag" in
228 "> $msg_view_details")
229 f_dialog_input_view_details
230 continue
231 esac
232 elif [ $retval -eq 2 ]; then
233 # The ``Help'' button (labeled "Details") was pressed
234 f_dialog_input_view_details
235 continue
236 fi
237
238 [ $retval -eq 0 ] || f_die
239
240 case "$mtag" in
241 "X $msg_exit") break ;;
242 "> $msg_add_new")
243 $BSDCFG_LIBE/$APP_DIR/rcadd ${USE_XDIALOG:+-X}
244 ;;
245 "> $msg_delete")
246 # rcdelete has a similar interface that can inherit the below:
247 export SHOW_VALUE SHOW_DESC SHOW_DEFAULT_VALUE SHOW_CONFIGURED
248 $BSDCFG_LIBE/$APP_DIR/rcdelete ${USE_XDIALOG:+-X}
249 ;;
250 *) # Anything else is a variable to edit
251 $BSDCFG_LIBE/$APP_DIR/rcedit ${USE_XDIALOG:+-X} "${mtag# }"
252 esac
253done
254
255exit $SUCCESS
256
257################################################################################
258# END
259################################################################################