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 __var_to_set="$1"
111 # If the in-memory cached value is available, return it immediately
112 if [ "$_STARTUP_RCCONF_MAP" ]; then
113 if [ "$__var_to_set" ]; then
114 setvar "$__var_to_set" "$STARTUP_RCCONF_MAP"
116 echo "$STARTUP_RCCONF_MAP"
122 # create the in-memory cache (potentially from validated on-disk cache)
126 # Calculate digest used to determine if the on-disk global persistant
127 # cache file (containing this digest on the first line) is valid and
128 # can be used to quickly populate the cache value for immediate return.
130 local __rc_defaults_digest
131 __rc_defaults_digest=$( md5 < "$RC_DEFAULTS" )
134 # Check to see if the global persistant cache file exists
136 if [ -f "$STARTUP_RCCONF_MAP_CACHEFILE" ]; then
138 # Attempt to populate the in-memory cache with the (soon to be)
139 # validated on-disk cache. If validation fails, fall-back to
140 # the current value and provide error exit status.
142 STARTUP_RCCONF_MAP=$(
143 ( # Get digest as the first word on the first line
144 read digest rest_ignored
147 # If the stored digest matches the calculated-
148 # one populate the in-memory cache from the on-
149 # disk cache and provide success exit status.
151 if [ "$digest" = "$__rc_defaults_digest" ]
156 # Otherwise, return the current value
157 echo "$STARTUP_RCCONF_MAP"
160 ) < "$STARTUP_RCCONF_MAP_CACHEFILE"
163 export STARTUP_RCCONF_MAP # Make children faster (export cache)
164 if [ $__retval -eq $SUCCESS ]; then
165 export _STARTUP_RCCONF_MAP=1
166 if [ "$__var_to_set" ]; then
167 setvar "$__var_to_set" "$STARTUP_RCCONF_MAP"
169 echo "$STARTUP_RCCONF_MAP"
173 # Otherwise, fall-thru to create in-memory cache from scratch
177 # If we reach this point, we need to generate the data from scratch
178 # (and after we do, we'll attempt to create the global persistant
179 # cache file to speed up future executions).
182 STARTUP_RCCONF_MAP=$(
183 f_clean_env --except \
186 STARTUP_RCCONF_REGEX \
190 # Unset variables we don't want reported
191 unset source_rc_confs_defined
193 for var in $( set | awk -F= "
194 function test_print(var)
196 if ( var == \"OPTIND\" ) return
197 if ( var == \"PATH\" ) return
198 if ( var == \"RC_DEFAULTS\" ) return
199 if ( var == \"STARTUP_RCCONF_REGEX\" ) return
200 if ( var == \"f_sysrc_desc_awk\" ) return
203 /$STARTUP_RCCONF_REGEX/ { test_print(\$1) }
205 echo $var "$( f_sysrc_desc $var )"
208 export STARTUP_RCCONF_MAP
209 export _STARTUP_RCCONF_MAP=1
210 if [ "$__var_to_set" ]; then
211 setvar "$__var_to_set" "$STARTUP_RCCONF_MAP"
213 echo "$STARTUP_RCCONF_MAP"
217 # Attempt to create the persistant global cache
220 # Create a new temporary file to write to
221 local __tmpfile="$( mktemp -t "$pgm" )"
222 [ "$__tmpfile" ] || return $FAILURE
224 # Write the temporary file contents
225 echo "$__rc_defaults_digest" > "$__tmpfile"
226 echo "$STARTUP_RCCONF_MAP" >> "$__tmpfile"
228 # Finally, move the temporary file into place
229 case "$STARTUP_RCCONF_MAP_CACHEFILE" in
230 */*) f_quietly mkdir -p "${STARTUP_RCCONF_MAP_CACHEFILE%/*}"
232 mv "$__tmpfile" "$STARTUP_RCCONF_MAP_CACHEFILE"
235 # f_startup_rcconf_map_expand $var_to_get
237 # Expands the map ($var_to_get) into the shell environment namespace by
238 # creating _${var}_desc variables containing the description of each variable
241 # NOTE: Variables are exported for later-required awk(1) ENVIRON visibility.
243 f_startup_rcconf_map_expand()
245 local var_to_get="$1"
246 eval "$( f_getvar "$var_to_get" | awk '
248 rword = "^[[:space:]]*[^[:space:]]*[[:space:]]*"
254 gsub(/'\''/, "'\''\\'\'\''", desc)
255 printf "_%s_desc='\''%s'\''\n", var, desc
256 printf "export _%s_desc\n", var
260 # f_dialog_input_view_details
262 # Display a menu for selecting which details are to be displayed. The following
263 # variables are tracked/modified by the menu/user's selection:
265 # SHOW_DESC Show or hide descriptions
267 # Mutually exclusive options:
269 # SHOW_VALUE Show the value (default; override only)
270 # SHOW_DEFAULT_VALUE Show both value and default
271 # SHOW_CONFIGURED Show rc.conf(5) file variable is configured in
273 # Each variable is treated as a boolean (NULL for false, non-NULL for true).
275 # Variables are exported for later-required awk(1) ENVIRON visibility. Returns
276 # success unless the user chose `Cancel' or pressed Escape.
278 f_dialog_input_view_details()
281 local menu_list # calculated below
282 local defaultitem= # calculated below
283 local hline="$hline_arrows_tab_enter"
285 # Calculate marks for checkboxes and radio buttons
287 if [ "$SHOW_DESC" ]; then
290 local m1=" " m2=" " m3=" "
291 if [ "$SHOW_VALUE" ]; then
293 defaultitem="1 ($m1) $msg_show_value"
294 elif [ "$SHOW_DEFAULT_VALUE" ]; then
296 defaultitem="2 ($m2) $msg_show_default_value"
297 elif [ "$SHOW_CONFIGURED" ]; then
299 defaultitem="3 ($m3) $msg_show_configured"
302 # Create the menu list with the above-calculated marks
304 'R $msg_reset' '$msg_reset_desc'
305 'D [$md] $msg_desc' '$msg_desc_desc'
306 '1 ($m1) $msg_show_value' '$msg_show_value_desc'
307 '2 ($m2) $msg_show_default_value' '$msg_show_default_value_desc'
308 '3 ($m3) $msg_show_configured' '$msg_show_configured_desc'
311 local height width rows
312 eval f_dialog_menu_size height width rows \
314 \"\$DIALOG_BACKTITLE\" \
319 f_dialog_title "$msg_choose_view_details"
322 mtag=$( eval $DIALOG \
323 --title \"\$DIALOG_TITLE\" \
324 --backtitle \"\$DIALOG_BACKTITLE\" \
325 --hline \"\$hline\" \
326 --ok-label \"\$msg_ok\" \
327 --cancel-label \"\$msg_cancel\" \
328 --default-item \"\$defaultitem\" \
329 --menu \"\$prompt\" \
330 $height $width $rows \
332 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
335 f_dialog_data_sanitize mtag
337 f_dialog_title_restore
339 [ $retval -eq $DIALOG_OK ] || return $DIALOG_CANCEL
348 "D [X] $msg_desc") SHOW_DESC= ;;
349 "D [ ] $msg_desc") SHOW_DESC=1 ;;
350 "1 ("?") $msg_show_value")
355 "2 ("?") $msg_show_default_value")
360 "3 ("?") $msg_show_configured")
368 # f_dialog_input_rclist [$default]
370 # Presents a menu of rc.conf(5) defaults (with, or without descriptions). This
371 # function should be treated like a call to dialog(1) (the exit status should
372 # be captured and f_dialog_menutag_fetch() should be used to get the user's
373 # response). Optionally if present and non-null, highlight $default rcvar.
375 f_dialog_input_rclist()
377 local prompt="$msg_please_select_an_rcconf_directive"
379 'X $msg_exit' '' ${SHOW_DESC:+'$msg_exit_this_menu'}
381 local defaultitem="$1"
382 local hline="$hline_arrows_tab_enter"
384 if [ ! "$_RCCONF_MAP" ]; then
385 # Generate RCCONF_MAP of `var desc ...' per-line
386 f_dialog_info "$msg_creating_rcconf_map"
387 RCCONF_MAP=$( f_startup_rcconf_map )
389 # Generate _${var}_desc variables from $RCCONF_MAP
390 f_startup_rcconf_map_expand
394 menu_list="$menu_list $(
396 echo "$RCCONF_MAP" | awk '
399 rword = "^[[:space:]]*[^[:space:]]*[[:space:]]*"
402 cur_prefix = tolower(substr($1, 1, 1))
404 if ( prefix != cur_prefix )
409 printf "%s'\'' '\'\''", rcvar
410 if ( ENVIRON["SHOW_DESC"] ) {
413 gsub(/'\''/, "'\''\\'\'\''", desc)
414 printf " '\''%s'\''", desc
420 set -f # set noglob because descriptions in the $menu_list may contain
421 # `*' and get expanded by dialog(1) (doesn't affect Xdialog(1)).
422 # This prevents dialog(1) from expanding wildcards in help line.
424 local height width rows
425 eval f_dialog_menu${SHOW_DESC:+_with_help}_size \
428 \"\$DIALOG_BACKTITLE\" \
434 menu_choice=$( eval $DIALOG \
435 --title \"\$DIALOG_TITLE\" \
436 --backtitle \"\$DIALOG_BACKTITLE\" \
437 --hline \"\$hline\" \
438 --default-item \"\$defaultitem\" \
439 --ok-label \"\$msg_ok\" \
440 --cancel-label \"\$msg_cancel\" \
441 ${SHOW_DESC:+--item-help} \
442 --menu \"\$prompt\" \
443 $height $width $rows \
445 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
448 f_dialog_menutag_store -s "$menu_choice"
452 # f_dialog_input_rcvar [$init]
454 # Allows the user to enter the name for a new rc.conf(5) variable. If the user
455 # does not cancel or press ESC, the $rcvar variable will hold the newly-
456 # configured value upon return.
458 f_dialog_input_rcvar()
461 # Loop until the user provides taint-free/valid input
466 # Return if user either pressed ESC or chosen Cancel/No
467 f_dialog_input _input "$msg_please_enter_rcvar_name" \
468 "$_input" "$hline_alnum_tab_enter" || return $?
470 # Check for invalid entry (1of2)
471 if ! echo "$_input" | grep -q "^[[:alpha:]_]"; then
472 f_show_msg "$msg_rcvar_must_start_with"
476 # Check for invalid entry (2of2)
477 if ! echo "$_input" | grep -q "^[[:alpha:]_][[:alnum:]_]*$"
479 f_show_msg "$msg_rcvar_contains_invalid_chars"
487 f_dprintf "f_dialog_input_rcvar: rcvar->[%s]" "$rcvar"
492 ############################################################ MAIN
494 f_dprintf "%s: Successfully loaded." startup/rcconf.subr
496 fi # ! $_STARTUP_RCCONF_SUBR