1 if [ ! "$_STARTUP_RCCONF_SUBR" ]; then _STARTUP_RCCONF_SUBR=1
3 # Copyright (c) 2006-2013 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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 (INCLUDING, 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
29 ############################################################ INCLUDES
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." startup/rcconf.subr
34 f_include $BSDCFG_SHARE/sysrc.subr
36 BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
37 f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
39 ############################################################ GLOBALS
42 # Initialize in-memory cache variables
48 # Define what a variable looks like
50 STARTUP_RCCONF_REGEX="^[[:alpha:]_][[:alnum:]_]*="
53 # Default path to on-disk cache file(s)
55 STARTUP_RCCONF_MAP_CACHEFILE="/var/run/bsdconfig/startup_rcconf_map.cache"
57 ############################################################ FUNCTIONS
59 # f_startup_rcconf_list
61 # Produce a list of non-default configuration variables configured in the
62 # rc.conf(5) collection of files.
64 f_startup_rcconf_list()
66 ( # Operate within a sub-shell to protect the parent environment
67 . "$RC_DEFAULTS" > /dev/null
68 f_clean_env --except PATH STARTUP_RCCONF_REGEX rc_conf_files
69 source_rc_confs > /dev/null
70 export _rc_conf_files_file="$( f_sysrc_find rc_conf_files )"
73 function test_print(var)
75 if ( var == \"OPTIND\" ) return
76 if ( var == \"PATH\" ) return
77 if ( var == \"RC_DEFAULTS\" ) return
78 if ( var == \"STARTUP_RCCONF_REGEX\" ) return
79 if ( var == \"_rc_conf_files_file\" ) return
80 if ( var == \"rc_conf_files\" )
82 if ( ENVIRON[\"_rc_conf_files_file\"] == \
83 ENVIRON[\"RC_DEFAULTS\"] ) return
87 /$STARTUP_RCCONF_REGEX/ { test_print(\$1) }"
91 # f_startup_rcconf_map [$var_to_set]
93 # Produce a map (beit from in-memory cache or on-disk cache) of rc.conf(5)
94 # variables and their descriptions. The map returned has the following format:
98 # With each as follows:
100 # var the rc.conf(5) variable
101 # description description of the variable
103 # If $var_to_set is missing or NULL, the map is printed to standard output for
104 # capturing in a sub-shell (which is less-recommended because of performance
105 # degredation; for example, when called in a loop).
107 f_startup_rcconf_map()
109 local __funcname=f_startup_rcconf_map
110 local __var_to_set="$1"
112 # If the in-memory cached value is available, return it immediately
113 if [ "$_STARTUP_RCCONF_MAP" ]; then
114 if [ "$__var_to_set" ]; then
115 setvar "$__var_to_set" "$STARTUP_RCCONF_MAP"
117 echo "$STARTUP_RCCONF_MAP"
123 # Create the in-memory cache (potentially from validated on-disk cache)
127 # Calculate digest used to determine if the on-disk global persistant
128 # cache file (containing this digest on the first line) is valid and
129 # can be used to quickly populate the cache value for immediate return.
131 local __rc_defaults_digest
132 __rc_defaults_digest=$( exec 2> /dev/null; md5 < "$RC_DEFAULTS" )
135 # Check to see if the global persistant cache file exists
137 if [ -f "$STARTUP_RCCONF_MAP_CACHEFILE" ]; then
139 # Attempt to populate the in-memory cache with the (soon to be)
140 # validated on-disk cache. If validation fails, fall-back to
141 # the current value and provide error exit status.
143 STARTUP_RCCONF_MAP=$(
144 ( # Get digest as the first word on first line
145 read digest rest_ignored
148 # If the stored digest matches the calculated-
149 # one populate the in-memory cache from the on-
150 # disk cache and provide success exit status.
152 if [ "$digest" = "$__rc_defaults_digest" ]
157 # Otherwise, return the current value
158 echo "$STARTUP_RCCONF_MAP"
161 ) < "$STARTUP_RCCONF_MAP_CACHEFILE"
164 export STARTUP_RCCONF_MAP # Make children faster (export cache)
165 if [ $__retval -eq $SUCCESS ]; then
166 export _STARTUP_RCCONF_MAP=1
167 if [ "$__var_to_set" ]; then
168 setvar "$__var_to_set" "$STARTUP_RCCONF_MAP"
170 echo "$STARTUP_RCCONF_MAP"
174 # Otherwise, fall-thru to create in-memory cache from scratch
178 # If we reach this point, we need to generate the data from scratch
179 # (and after we do, we'll attempt to create the global persistant
180 # cache file to speed up future executions).
183 STARTUP_RCCONF_MAP=$(
184 f_clean_env --except \
187 STARTUP_RCCONF_REGEX \
191 # Unset variables we don't want reported
192 unset source_rc_confs_defined
194 for var in $( set | awk -F= "
195 function test_print(var)
197 if ( var == \"OPTIND\" ) return
198 if ( var == \"PATH\" ) return
199 if ( var == \"RC_DEFAULTS\" ) return
200 if ( var == \"STARTUP_RCCONF_REGEX\" ) return
201 if ( var == \"f_sysrc_desc_awk\" ) return
204 /$STARTUP_RCCONF_REGEX/ { test_print(\$1) }
206 echo $var "$( f_sysrc_desc $var )"
209 export STARTUP_RCCONF_MAP
210 export _STARTUP_RCCONF_MAP=1
211 if [ "$__var_to_set" ]; then
212 setvar "$__var_to_set" "$STARTUP_RCCONF_MAP"
214 echo "$STARTUP_RCCONF_MAP"
218 # Attempt to create the persistant global cache
221 # Create a new temporary file to write to
223 f_eval_catch -dk __tmpfile $__funcname mktemp \
224 'mktemp -t "%s"' "$pgm" || return $FAILURE
226 # Write the temporary file contents
227 echo "$__rc_defaults_digest" > "$__tmpfile"
228 echo "$STARTUP_RCCONF_MAP" >> "$__tmpfile"
230 # Finally, move the temporary file into place
231 case "$STARTUP_RCCONF_MAP_CACHEFILE" in
232 */*) f_eval_catch -d $__funcname mkdir \
233 'mkdir -p "%s"' "${STARTUP_RCCONF_MAP_CACHEFILE%/*}"
235 f_eval_catch -d $__funcname mv \
236 'mv "%s" "%s"' "$__tmpfile" "$STARTUP_RCCONF_MAP_CACHEFILE"
239 # f_startup_rcconf_map_expand $var_to_get
241 # Expands the map ($var_to_get) into the shell environment namespace by
242 # creating _${var}_desc variables containing the description of each variable
245 # NOTE: Variables are exported for later-required awk(1) ENVIRON visibility.
247 f_startup_rcconf_map_expand()
249 local var_to_get="$1"
250 eval "$( debug= f_getvar "$var_to_get" | awk '
252 rword = "^[[:space:]]*[^[:space:]]*[[:space:]]*"
258 gsub(/'\''/, "'\''\\'\'\''", desc)
259 printf "_%s_desc='\''%s'\''\n", var, desc
260 printf "export _%s_desc\n", var
264 # f_dialog_input_view_details
266 # Display a menu for selecting which details are to be displayed. The following
267 # variables are tracked/modified by the menu/user's selection:
269 # SHOW_DESC Show or hide descriptions
271 # Mutually exclusive options:
273 # SHOW_VALUE Show the value (default; override only)
274 # SHOW_DEFAULT_VALUE Show both value and default
275 # SHOW_CONFIGURED Show rc.conf(5) file variable is configured in
277 # Each variable is treated as a boolean (NULL for false, non-NULL for true).
279 # Variables are exported for later-required awk(1) ENVIRON visibility. Returns
280 # success unless the user chose `Cancel' or pressed Escape.
282 f_dialog_input_view_details()
285 local menu_list # calculated below
286 local defaultitem= # calculated below
287 local hline="$hline_arrows_tab_enter"
289 # Calculate marks for checkboxes and radio buttons
291 if [ "$SHOW_DESC" ]; then
294 local m1=" " m2=" " m3=" "
295 if [ "$SHOW_VALUE" ]; then
297 defaultitem="1 ($m1) $msg_show_value"
298 elif [ "$SHOW_DEFAULT_VALUE" ]; then
300 defaultitem="2 ($m2) $msg_show_default_value"
301 elif [ "$SHOW_CONFIGURED" ]; then
303 defaultitem="3 ($m3) $msg_show_configured"
306 # Create the menu list with the above-calculated marks
308 'R $msg_reset' '$msg_reset_desc'
309 'D [$md] $msg_desc' '$msg_desc_desc'
310 '1 ($m1) $msg_show_value' '$msg_show_value_desc'
311 '2 ($m2) $msg_show_default_value' '$msg_show_default_value_desc'
312 '3 ($m3) $msg_show_configured' '$msg_show_configured_desc'
315 local height width rows
316 eval f_dialog_menu_size height width rows \
318 \"\$DIALOG_BACKTITLE\" \
323 f_dialog_title "$msg_choose_view_details"
326 mtag=$( eval $DIALOG \
327 --title \"\$DIALOG_TITLE\" \
328 --backtitle \"\$DIALOG_BACKTITLE\" \
329 --hline \"\$hline\" \
330 --ok-label \"\$msg_ok\" \
331 --cancel-label \"\$msg_cancel\" \
332 --default-item \"\$defaultitem\" \
333 --menu \"\$prompt\" \
334 $height $width $rows \
336 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
339 f_dialog_data_sanitize mtag
341 f_dialog_title_restore
343 [ $retval -eq $DIALOG_OK ] || return $DIALOG_CANCEL
352 "D [X] $msg_desc") SHOW_DESC= ;;
353 "D [ ] $msg_desc") SHOW_DESC=1 ;;
354 "1 ("?") $msg_show_value")
359 "2 ("?") $msg_show_default_value")
364 "3 ("?") $msg_show_configured")
372 # f_dialog_input_rclist [$default]
374 # Presents a menu of rc.conf(5) defaults (with, or without descriptions). This
375 # function should be treated like a call to dialog(1) (the exit status should
376 # be captured and f_dialog_menutag_fetch() should be used to get the user's
377 # response). Optionally if present and non-null, highlight $default rcvar.
379 f_dialog_input_rclist()
381 local prompt="$msg_please_select_an_rcconf_directive"
383 'X $msg_exit' '' ${SHOW_DESC:+'$msg_exit_this_menu'}
385 local defaultitem="$1"
386 local hline="$hline_arrows_tab_enter"
388 if [ ! "$_RCCONF_MAP" ]; then
389 # Generate RCCONF_MAP of `var desc ...' per-line
390 f_dialog_info "$msg_creating_rcconf_map"
391 RCCONF_MAP=$( f_startup_rcconf_map )
393 # Generate _${var}_desc variables from $RCCONF_MAP
394 f_startup_rcconf_map_expand
398 menu_list="$menu_list $(
400 echo "$RCCONF_MAP" | awk '
403 rword = "^[[:space:]]*[^[:space:]]*[[:space:]]*"
406 cur_prefix = tolower(substr($1, 1, 1))
408 if ( prefix != cur_prefix )
413 printf "%s'\'' '\'\''", rcvar
414 if ( ENVIRON["SHOW_DESC"] ) {
417 gsub(/'\''/, "'\''\\'\'\''", desc)
418 printf " '\''%s'\''", desc
424 set -f # set noglob because descriptions in the $menu_list may contain
425 # `*' and get expanded by dialog(1) (doesn't affect Xdialog(1)).
426 # This prevents dialog(1) from expanding wildcards in help line.
428 local height width rows
429 eval f_dialog_menu${SHOW_DESC:+_with_help}_size \
432 \"\$DIALOG_BACKTITLE\" \
438 menu_choice=$( eval $DIALOG \
439 --title \"\$DIALOG_TITLE\" \
440 --backtitle \"\$DIALOG_BACKTITLE\" \
441 --hline \"\$hline\" \
442 --default-item \"\$defaultitem\" \
443 --ok-label \"\$msg_ok\" \
444 --cancel-label \"\$msg_cancel\" \
445 ${SHOW_DESC:+--item-help} \
446 --menu \"\$prompt\" \
447 $height $width $rows \
449 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
452 f_dialog_menutag_store -s "$menu_choice"
456 # f_dialog_input_rcvar [$init]
458 # Allows the user to enter the name for a new rc.conf(5) variable. If the user
459 # does not cancel or press ESC, the $rcvar variable will hold the newly-
460 # configured value upon return.
462 f_dialog_input_rcvar()
465 # Loop until the user provides taint-free/valid input
470 # Return if user either pressed ESC or chosen Cancel/No
471 f_dialog_input _input "$msg_please_enter_rcvar_name" \
472 "$_input" "$hline_alnum_tab_enter" || return $?
474 # Check for invalid entry (1of2)
475 if ! echo "$_input" | grep -q "^[[:alpha:]_]"; then
476 f_show_msg "$msg_rcvar_must_start_with"
480 # Check for invalid entry (2of2)
481 if ! echo "$_input" | grep -q "^[[:alpha:]_][[:alnum:]_]*$"
483 f_show_msg "$msg_rcvar_contains_invalid_chars"
491 f_dprintf "f_dialog_input_rcvar: rcvar->[%s]" "$rcvar"
496 ############################################################ MAIN
498 f_dprintf "%s: Successfully loaded." startup/rcconf.subr
500 fi # ! $_STARTUP_RCCONF_SUBR