git: b5e5c857e9b4 - main - sysrc: Add .= operator for STRAPPEND

From: Devin Teske <dteske_at_FreeBSD.org>
Date: Wed, 18 Nov 2026 16:41:58 UTC
The branch main has been updated by dteske:

URL: https://cgit.FreeBSD.org/src/commit/?id=b5e5c857e9b4d99fb2af9375942e02ecf78ad416

commit b5e5c857e9b4d99fb2af9375942e02ecf78ad416
Author:     Devin Teske <dteske@FreeBSD.org>
AuthorDate: 2026-11-18 16:38:06 +0000
Commit:     Devin Teske <dteske@FreeBSD.org>
CommitDate: 2026-11-18 16:38:06 +0000

    sysrc: Add .= operator for STRAPPEND
    
    There are almost 100 *_flags variables in /etc/defaults/rc.conf and
    each holds a set of options processed by getopts in one way or
    another. The temptation to manage these `*_flags` variables with the
    list operator += leads to potentially unexpected behavior.
    
    For example, let's say you have in rc.conf:
    
    cron_flags="$cron_flags -j 15"
    
    Executing `sysrc cron_flags+="-J 15"` to set rootjitter to 15 will
    instead result in:
    
    cron_flags="$cron_flags -j 15 -J"
    
    That's because += is not a string append operation but a list add
    operation that adds -J but, seeing that 15 is already in the space-
    delimited "list," does not add "15".
    
    Despite the fact that one could rely on the fact that getopts allows
    `-J15` in place of `-J 15`, we should probably just allow for string
    append operations.
    
    Borrowing the `.=` string-append operator from Perl, this implements
    a method to conveniently append text to a string value such as all
    the *_flags variables. Executing `sysrc cron_flags.="-J 15"` would
    result in:
    
    cron_flags="$cron_flags -j 15 -J 15"
    
    As a matter of convenience, a single space is added after a non-NULL
    value. If you need to append directly to the value without a space,
    you can force the use of any delimiter (or no delimiter) by first
    collecting the raw value and then setting it in the desired manner.
    
    For example:
    
    cur=$( sysrc -inr foo )
    sysrc foo="$cur${cur:+,}extra"
    
    The above has always worked and continues to work. The new .=
    operator is equivalent to:
    
    cur=$( sysrc -inr foo )
    sysrc foo="$cur${cur:+ }extra" # same as sysrc foo.=extra
    
    Reviewed by:    kfv
    Differential Revision:  https://reviews.freebsd.org/D61659
---
 usr.sbin/sysrc/sysrc               | 24 +++++++----
 usr.sbin/sysrc/sysrc.8             | 82 +++++++++++++++++++++++++++++++++++---
 usr.sbin/sysrc/tests/sysrc_test.sh | 51 ++++++++++++++++++++++++
 3 files changed, 145 insertions(+), 12 deletions(-)

diff --git a/usr.sbin/sysrc/sysrc b/usr.sbin/sysrc/sysrc
index 54939cad0b96..dc3a3e39cf3b 100644
--- a/usr.sbin/sysrc/sysrc
+++ b/usr.sbin/sysrc/sysrc
@@ -16,7 +16,7 @@ pgm="${0##*/}" # Program basename
 #
 # Version information
 #
-SYSRC_VERSION="8.2 Nov-16,2026"
+SYSRC_VERSION="8.3 Nov-16,2026"
 
 #
 # Options
@@ -63,7 +63,7 @@ die()
 usage()
 {
 	f_err "Usage: %s [OPTIONS] %s\n" "$pgm" \
-		"{name[[+|-]=value] ... | -a | -A | -l | -L [name ...]}"
+		"{name[[+|-|.]=value] ... | -a | -A | -l | -L [name ...]}"
 	f_err "Try \`%s --help' for more information.\n" "$pgm"
 	die
 }
@@ -77,7 +77,7 @@ help()
 	local optfmt="\t%-11s%s\n"
 	local envfmt="\t%-17s%s\n"
 
-	f_err "Usage: %s [OPTIONS] name[[+|-]=value] ...\n" "$pgm"
+	f_err "Usage: %s [OPTIONS] name[[+|-|.]=value] ...\n" "$pgm"
 	f_err "Usage: %s [OPTIONS] -a | -A\n" "$pgm"
 	f_err "Usage: %s [OPTIONS] -l | -L [name ...]\n" "$pgm"
 
@@ -332,6 +332,7 @@ fi
 for name in "$@"; do
 	# NB: shell expansion syntax removed first
 	name="${name%%:[+=-]*}"
+	name="${name%%.=*}" # string append
 	name="${name%%[%#+=-]*}"
 	[ "$name" = "${name#*[!$VALID_VARNAME_CHARS]}" ] || die \
 		"%s: %s: name contains characters not allowed in shell" \
@@ -688,8 +689,9 @@ while [ $# -gt 0 ]; do
 	NAME="${1%%=*}"
 
 	case "$NAME" in
-	*+) mode=APPEND NAME="${NAME%+}" ;;
-	*-) mode=REMOVE NAME="${NAME%-}" ;;
+	*+) mode=LISTAPPEND NAME="${NAME%+}" ;;
+	*-) mode=LISTREMOVE NAME="${NAME%-}" ;;
+	*.) mode=STRAPPEND NAME="${NAME%.}" ;;
 	 *) mode=ASSIGN
 	esac
 
@@ -729,7 +731,7 @@ while [ $# -gt 0 ]; do
 		# Determine both `before' value and appropriate `new' value
 		#
 		case "$mode" in
-		APPEND)
+		LISTAPPEND)
 			before=$( f_sysrc_get_raw "$NAME" )
 			add="${1#*=}"
 			delim="${add%"${add#?}"}" # first character
@@ -753,7 +755,7 @@ while [ $# -gt 0 ]; do
 			unset add delim oldIFS a skip b
 			[ "$SHOW_FILE" ] && before=$( f_sysrc_find "$NAME" )
 			;;
-		REMOVE)
+		LISTREMOVE)
 			before=$( f_sysrc_get_raw "$NAME" )
 			remove="${1#*=}"
 			delim="${remove%"${remove#?}"}" # first character
@@ -777,6 +779,14 @@ while [ $# -gt 0 ]; do
 			unset remove delim oldIFS b add r
 			[ "$SHOW_FILE" ] && before=$( f_sysrc_find "$NAME" )
 			;;
+		STRAPPEND)
+			before=$( f_sysrc_get_raw "$NAME" )
+			add="${1#*=}"
+			new="$add"
+			[ "$before" ] && new="$before $add"
+			unset add
+			[ "$SHOW_FILE" ] && before=$( f_sysrc_find "$NAME" )
+			;;
 		*) # ASSIGN
 			if [ "$SHOW_FILE" ]; then
 				before=$( f_sysrc_find "$NAME" )
diff --git a/usr.sbin/sysrc/sysrc.8 b/usr.sbin/sysrc/sysrc.8
index e0330bfd3769..97fbc265ff4c 100644
--- a/usr.sbin/sysrc/sysrc.8
+++ b/usr.sbin/sysrc/sysrc.8
@@ -25,7 +25,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd November 15, 2026
+.Dd November 17, 2026
 .Dt SYSRC 8
 .Os
 .Sh NAME
@@ -37,7 +37,7 @@
 .Op Fl s Ar name
 .Op Fl f Ar file
 .Op Fl j Ar jail | Fl R Ar dir
-.Ar name Ns Op Ns Oo +|- Oc Ns = Ns Ar value
+.Ar name Ns Op Ns Oo +|-|. Oc Ns = Ns Ar value
 .Ar ...
 .Nm
 .Op Fl cdDeEFhinNqrvx
@@ -200,11 +200,14 @@ In addition
 .Pq but unlike Xr sysctl 8 ,
 .Ql name+=value
 is supported for adding items to values
-.Pq see APPENDING VALUES
-and
+.Pq see APPENDING VALUES ,
 .Ql name-=value
 is supported for removing items from values
-.Pq see SUBTRACTING VALUES .
+.Pq see SUBTRACTING VALUES ,
+and
+.Ql name.=value
+is supported for appending text
+.Pq see APPENDING TEXT .
 .Pp
 However, while
 .Xr sysctl 8
@@ -379,6 +382,69 @@ foo-=baz # `baz' -> NULL
 .Nm
 removes all occurrences of all items provided
 and collapses extra delimiters between items.
+.Sh APPENDING TEXT
+When using the
+.Ql key.=value
+syntax,
+.Nm
+appends
+.Ar value
+to the current value as one string.
+The first character is not a delimiter, and
+.Ar value
+is not split into items.
+If the current value is non-NULL, one space is inserted before the text.
+If no
+.Ar value
+is given
+.Pq Ql key.= ,
+nothing is changed and success is returned.
+.Pp
+The text operator
+.Ql .=
+suits option strings stored in
+.Ql *_flags
+variables.
+For example, with
+.Va cron_flags
+set to
+.Dq $cron_flags -j 15 :
+.Bl -item -offset indent
+.It
+.Nm
+cron_flags.="-J 15"
+.Dl # `$cron_flags -j 15' -> `$cron_flags -j 15 -J 15'
+.El
+.Pp
+The list operator
+.Ql +=
+does not do this
+.Pq see Sx APPENDING VALUES .
+.Ql cron_flags+="-J 15"
+has two items,
+.Dq -J
+and
+.Dq 15 .
+Because
+.Dq 15
+is already present, only
+.Dq -J
+is added, and the stored text becomes
+.Dq $cron_flags -j 15 -J .
+.Pp
+To join with another delimiter, or with none, manually read the raw value and
+then assign the desired result.
+With a comma:
+.Pp
+.Dl cur=$( sysrc -inr foo\  )
+.Dl sysrc foo="$cur${cur:+,}extra"
+.Pp
+The
+.Ql .=
+operator is equivalent to:
+.Pp
+.Dl cur=$( sysrc -inr foo\  )
+.Dl sysrc foo="$cur${cur:+ }extra"
 .Sh ENVIRONMENT
 The following environment variables are referenced by
 .Nm :
@@ -448,6 +514,12 @@ Remove
 from $cloned_interfaces
 .Pq see Sx SUBTRACTING VALUES :
 .Dl sysrc cloned_interfaces-=gif0
+.Pp
+Append
+.Dq -J 15
+to $cron_flags
+.Pq see Sx APPENDING TEXT :
+.Dl sysrc cron_flags.="-J 15"
 .Ss Inline shell parameter expansion
 Return $hostname up to, but not including, first
 .Ql \&. :
diff --git a/usr.sbin/sysrc/tests/sysrc_test.sh b/usr.sbin/sysrc/tests/sysrc_test.sh
index d284207313bc..676cf26f48e4 100644
--- a/usr.sbin/sysrc/tests/sysrc_test.sh
+++ b/usr.sbin/sysrc/tests/sysrc_test.sh
@@ -434,12 +434,63 @@ r_flag_body()
 	atf_check -o inline:'test_flags="-J 16"\n' cat "${SYSRC_CONF}"
 }
 
+atf_test_case strappend
+strappend_head()
+{
+	atf_set "descr" "String-append with .= inserts a space"
+}
+strappend_body()
+{
+	local msg
+
+	# NULL value: no leading space
+	atf_check -o inline:'cron_flags:  -> -J 15\n' \
+	    sysrc -f "${SYSRC_CONF}" 'cron_flags.=-J 15'
+	atf_check -o inline:'cron_flags="-J 15"\n' cat "${SYSRC_CONF}"
+
+	printf '%s\n' 'cron_flags="$cron_flags"' > "${SYSRC_CONF}"
+
+	# A space is inserted. The text is not scanned for a delimiter
+	msg='cron_flags: $cron_flags -> $cron_flags -J 15'
+	atf_check -o inline:"$msg\n" \
+	    sysrc -f "${SYSRC_CONF}" 'cron_flags.=-J 15'
+	atf_check -o inline:'cron_flags="$cron_flags -J 15"\n' \
+	    cat "${SYSRC_CONF}"
+
+	# A second append adds the text again, with another space
+	msg='cron_flags: $cron_flags -J 15 ->'
+	msg="$msg "'$cron_flags -J 15 -J 15'
+	atf_check -o inline:"$msg\n" \
+	    sysrc -f "${SYSRC_CONF}" 'cron_flags.=-J 15'
+
+	# -c reports the result and does not write
+	printf '%s\n' 'cron_flags="$cron_flags"' > "${SYSRC_CONF}"
+	msg="${SYSRC_CONF}: cron_flags: would change from"
+	msg="$msg \`\$cron_flags' to \`\$cron_flags -J 15'"
+	atf_check -s exit:1 -o inline:"$msg\n" \
+	    sysrc -f "${SYSRC_CONF}" -v -c 'cron_flags.=-J 15'
+	atf_check -o inline:'cron_flags="$cron_flags"\n' \
+	    cat "${SYSRC_CONF}"
+
+	# A comma stays text. .= does not take it as a delimiter
+	printf '%s\n' 'foo="bar"' > "${SYSRC_CONF}"
+	atf_check -o inline:'foo: bar -> bar ,extra\n' \
+	    sysrc -f "${SYSRC_CONF}" 'foo.=,extra'
+	atf_check -o inline:'foo="bar ,extra"\n' cat "${SYSRC_CONF}"
+
+	# += is still an exclusive list: -J is present, 15 is not
+	printf '%s\n' 'cron_flags="-J 10"' > "${SYSRC_CONF}"
+	atf_check -o inline:'cron_flags: -J 10 -> -J 10 15\n' \
+	    sysrc -f "${SYSRC_CONF}" 'cron_flags+=-J 15'
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case check_variable
 	atf_add_test_case set_variable
 	atf_add_test_case remove_variable_x_flag
 	atf_add_test_case append_list
+	atf_add_test_case strappend
 	atf_add_test_case subtract_list
 	atf_add_test_case multiple_operations
 	atf_add_test_case a_flag