Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F178107897
D62163.1795981863.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
8 KB
Referenced Files
None
Subscribers
None
D62163.1795981863.diff
View Options
diff --git a/usr.sbin/sysrc/sysrc b/usr.sbin/sysrc/sysrc
--- a/usr.sbin/sysrc/sysrc
+++ b/usr.sbin/sysrc/sysrc
@@ -16,7 +16,7 @@
#
# Version information
#
-SYSRC_VERSION="9.3.4 Nov-27,2026"
+SYSRC_VERSION="9.3.5 Nov-27,2026"
#
# Options
@@ -797,8 +797,17 @@
# If verbose, prefix line with where the directive lives
if [ "$VERBOSE" -a ! "$CHECK_ONLY" ]; then
file=$( f_sysrc_find "$NAME" )
- [ "$file" = "$RC_DEFAULTS" -o ! "$file" ] &&
- file=$( f_sysrc_get 'rc_conf_files%%[$IFS]*' )
+ if [ "$file" = "$RC_DEFAULTS" -o ! "$file" ]; then
+ #
+ # NB: f_sysrc_first will expand globs found in
+ # RC_CONFS or rc_conf_files, skipping any
+ # globs that don't match anything.
+ #
+ set -f # noglob
+ f_sysrc_first file ${RC_CONFS-$(
+ f_sysrc_get rc_conf_files )}
+ set +f # glob
+ fi
if [ "$SHOW_EQUALS" ]; then
shell_fmt "_file_$NAME=%s " "$file"
else
diff --git a/usr.sbin/sysrc/sysrc.subr b/usr.sbin/sysrc/sysrc.subr
--- a/usr.sbin/sysrc/sysrc.subr
+++ b/usr.sbin/sysrc/sysrc.subr
@@ -28,6 +28,7 @@
# Error messages
#
msg_cannot_create_permission_denied="%s: cannot create %s: Permission denied"
+msg_no_such_file_or_directory="%s: %s: No such file or directory"
msg_permission_denied="%s: %s: Permission denied"
msg_previous_syntax_errors="%s: Not overwriting \`%s' due to previous syntax errors"
msg_awk_failure="%s: Not overwriting \`%s' due to awk(1) failure"
@@ -1090,6 +1091,111 @@
awk -v varname="$1" "$f_sysrc_desc_awk" < "$RC_DEFAULTS"
}
+# f_sysrc_exp $var_to_set [$path ...]
+#
+# Expand unexpanded words into $var_to_set, separated by spaces. A
+# backslash escapes the next character. An unescaped glob that matches
+# nothing is omitted. A literal is stored even if new. Nothing left
+# stores an empty value.
+#
+f_sysrc_exp()
+{
+ local __exp_var_to_set="$1"
+ local __exp_word __exp_remain
+ local __exp_literal __exp_pattern
+ local __exp_before __exp_after
+ local __exp_char __exp_hasglob
+ local __exp_match __exp_noglob=
+ local __exp_result=
+
+ shift 1 # var_to_set
+
+ #
+ # Determine if caller has set noglob before we modify filename
+ # globbing, so we can later return to the inherited state.
+ #
+ case "$-" in *f*) __exp_noglob=1 ;; esac
+ for __exp_word in "$@"; do
+ # literal is the real filename; pattern is what we later glob
+ __exp_remain="$__exp_word"
+ __exp_literal=
+ __exp_pattern=
+
+ #
+ # An odd run of backslashes escapes the next character. Copy
+ # that character into the literal filename. In the pattern,
+ # escaped * ? and [ become a one-character class so they are
+ # not magic when the pattern is later globbed.
+ #
+ while case "$__exp_remain" in
+ *\\*) : escape remains ;;
+ *) false
+ esac
+ do
+ __exp_after="${__exp_remain#*\\}"
+ __exp_before="${__exp_remain%%\\*}"
+ __exp_char="${__exp_after#?}"
+ __exp_char="${__exp_after%"$__exp_char"}"
+ __exp_literal="$__exp_literal$__exp_before"
+ __exp_literal="$__exp_literal$__exp_char"
+ __exp_pattern="$__exp_pattern$__exp_before"
+ __exp_remain="${__exp_after#?}"
+ # * ? and [ must not stay magic in the pattern
+ case "$__exp_char" in
+ \*) __exp_pattern="$__exp_pattern[*]" ;;
+ \?) __exp_pattern="$__exp_pattern[?]" ;;
+ \[) __exp_pattern="$__exp_pattern[[]" ;;
+ *) __exp_pattern="$__exp_pattern$__exp_char"
+ esac
+ done
+
+ #
+ # The remainder has no backslash. It joins the literal name.
+ # * ? and a closed [...] are still globs. An unclosed [ is a
+ # literal filename, as is ].
+ #
+ __exp_literal="$__exp_literal$__exp_remain"
+ __exp_hasglob=
+ case "$__exp_remain" in
+ *[?*]*|*\[*\]*) __exp_hasglob=1 ;;
+ esac
+
+ #
+ # A literal is stored even if the file does not exist yet.
+ # An unmatched glob is skipped, so we do not create a file
+ # whose name is the pattern.
+ #
+ if [ ! "$__exp_hasglob" ]; then
+ __exp_result="$__exp_result $__exp_literal"
+ continue
+ fi
+ __exp_pattern="$__exp_pattern$__exp_remain"
+ set +f # glob
+ for __exp_match in $__exp_pattern; do
+ [ -e "$__exp_match" ] || continue
+ __exp_result="$__exp_result $__exp_match"
+ done
+ [ "$__exp_noglob" ] && set -f # noglob
+ done
+
+ setvar "$__exp_var_to_set" "${__exp_result# }"
+}
+
+# f_sysrc_first $var_to_set [$path ...]
+#
+# Store the first pathname from f_sysrc_exp. Nothing left stores an
+# empty value.
+#
+f_sysrc_first()
+{
+ local __var_to_set="$1"
+ local __paths
+
+ shift 1 # var_to_set
+ f_sysrc_exp __paths "$@"
+ setvar "$__var_to_set" "${__paths%%[$IFS]*}"
+}
+
# f_sysrc_set $varname $new_value
#
# Change a setting in the system configuration files (edits the files in-place
@@ -1191,23 +1297,26 @@
#
local not_found=
local file="$( f_sysrc_find "$varname" )"
+ local list
+ local noglob=
if [ "$file" = "$RC_DEFAULTS" -o ! "$file" ]; then
#
# We either got a null response (not found) or the variable
# was only found in the rc.conf(5) defaults. In either case,
# let's instead modify the first file from $rc_conf_files.
#
-
not_found=1
-
- #
- # If RC_CONFS is defined, use $RC_CONFS
- # rather than $rc_conf_files.
- #
- if [ "${RC_CONFS+set}" ]; then
- file="${RC_CONFS%%[$IFS]*}"
- else
- file=$( f_sysrc_get 'rc_conf_files%%[$IFS]*' )
+ list="${RC_CONFS-$( f_sysrc_get rc_conf_files )}"
+
+ case "$-" in *f*) noglob=1 ;; esac
+ # NB: Pass the words unexpanded. Backslashes escape globs
+ # NB: f_sysrc_first will expand globs, skipping unmatched
+ set -f # noglob
+ f_sysrc_first file $list
+ [ "$noglob" ] || set +f # glob
+ if [ ! "$file" ]; then
+ f_err "$msg_no_such_file_or_directory\n" "$pgm" "$list"
+ return $FAILURE
fi
fi
diff --git a/usr.sbin/sysrc/tests/sysrc_test.sh b/usr.sbin/sysrc/tests/sysrc_test.sh
--- a/usr.sbin/sysrc/tests/sysrc_test.sh
+++ b/usr.sbin/sysrc/tests/sysrc_test.sh
@@ -506,6 +506,58 @@
unset RC_DEFAULTS
}
+atf_test_case rc_conf_files_redef
+rc_conf_files_redef_head()
+{
+ atf_set "descr" "source_rc_confs second walk globs a redefined list"
+}
+rc_conf_files_redef_body()
+{
+ local late
+
+ mkdir rc.conf.d more.d
+ late="${PWD}/more.d/late.conf"
+ printf '%s\n' 'base="from-rc"' \
+ "rc_conf_files=\"${PWD}/rc.conf ${PWD}/more.d/*.conf\"" \
+ > "${PWD}/rc.conf"
+ printf '%s\n' 'gvar="from-glob"' > "${PWD}/rc.conf.d/extra.conf"
+ printf '%s\n' 'late="from-second"' > "$late"
+
+ RC_DEFAULTS="${PWD}/rc_defaults.conf"
+ cat > "${RC_DEFAULTS}" << EOF
+source_rc_confs() {
+ local i sourced_files
+ for i in \${rc_conf_files}; do
+ case \${sourced_files} in
+ *:\$i:*) : already sourced ;;
+ *)
+ sourced_files="\${sourced_files}:\$i:"
+ [ -r "\$i" ] && . "\$i"
+ ;;
+ esac
+ done
+ for i in \${rc_conf_files}; do
+ case \${sourced_files} in
+ *:\$i:*) : already sourced ;;
+ *)
+ sourced_files="\${sourced_files}:\$i:"
+ [ -r "\$i" ] && . "\$i"
+ ;;
+ esac
+ done
+}
+rc_conf_files="${PWD}/rc.conf ${PWD}/rc.conf.d/*.conf"
+EOF
+ export RC_DEFAULTS
+
+ atf_check -o inline:'gvar: from-glob\n' sysrc gvar
+ atf_check -o inline:'late: from-second\n' sysrc late
+}
+rc_conf_files_redef_cleanup()
+{
+ unset RC_DEFAULTS
+}
+
atf_test_case local_startup_glob cleanup
local_startup_glob_head()
{
@@ -555,6 +607,50 @@
unset RC_DEFAULTS
}
+atf_test_case firstfile_glob
+firstfile_glob_head()
+{
+ atf_set "descr" "A new variable is appended to the first real file"
+}
+firstfile_glob_body()
+{
+ local dir
+
+ dir="${PWD}/rc.conf.d"
+ mkdir "$dir"
+ printf '%s\n' 'keep="1"' > "${dir}/a.conf"
+ printf '%s\n' 'keep="1"' > "${dir}/b.conf"
+ printf '%s\n' 'keep="1"' > "${PWD}/real.conf"
+
+ atf_check -o inline:'newvar: -> x\n' \
+ sysrc -f "${dir}/*.conf" newvar=x
+ atf_check -o inline:'keep="1"\nnewvar="x"\n' cat "${dir}/a.conf"
+ atf_check test ! -e "${dir}/*.conf"
+
+ atf_check -o inline:'othervar: -> y\n' \
+ sysrc -f "${PWD}/no-such/*.conf ${PWD}/real.conf" othervar=y
+ atf_check -o inline:'keep="1"\nothervar="y"\n' cat "${PWD}/real.conf"
+ atf_check test ! -e "${PWD}/no-such/*.conf"
+
+ atf_check -s exit:1 \
+ -e inline:"sysrc: ${PWD}/missing/*.conf: No such file or directory\n" \
+ sysrc -f "${PWD}/missing/*.conf" nowhere=z
+ atf_check test ! -e "${PWD}/missing/*.conf"
+
+ atf_check -o inline:'fresh: -> 1\n' \
+ sysrc -f "${PWD}/brandnew.conf" fresh=1
+ atf_check -o inline:'fresh="1"\n' cat "${PWD}/brandnew.conf"
+
+ atf_check -o inline:'esc: -> \n' sysrc -f 'foo\*' esc=1
+ atf_check -o inline:'esc="1"\n' cat 'foo*'
+ atf_check test ! -e 'foo\*'
+
+ atf_check -s exit:1 \
+ -e inline:'sysrc: no-such*: No such file or directory\n' \
+ sysrc -f 'no-such*' miss=1
+ atf_check test ! -e 'no-such*'
+}
+
atf_test_case r_flag
r_flag_head()
{
@@ -745,6 +841,8 @@
atf_add_test_case F_flag
atf_add_test_case F_flag_assign
atf_add_test_case rc_conf_files_glob
+ atf_add_test_case rc_conf_files_redef
atf_add_test_case local_startup_glob
+ atf_add_test_case firstfile_glob
atf_add_test_case r_flag
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Nov 29, 7:51 PM (19 h, 59 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42087075
Default Alt Text
D62163.1795981863.diff (8 KB)
Attached To
Mode
D62163: sysrc: Fix edge case in all prior versions
Attached
Detach File
Event Timeline