From nobody Wed Nov 18 16:41:58 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jV6nf6vM9z6w65R for ; Wed, 18 Nov 2026 16:41:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jV6nf40Nqz4Tt3 for ; Wed, 18 Nov 2026 16:41:58 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795020118; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Rm9JqdRnwh09ZaYgOmxmZYJ6xE83Shr9CrwBXMfp0PI=; b=jUQMpPrcGQwmJHu1OgEpChNDmUe5yYe9d/qoEHks9MvDR9nXyAIVEdxvz9YLLwb1nO6NA6 88ngKnTYn35wFiKPoYLI9FZXebL0OQ9bm561I5IhIVVRZYX6BDFiflVKsEV4CvWywsCACq ZayxfMX+qILIDphDlGsbfEB+I4TnTPSXf2Sg2pCtWu6/4RYp5T5fI0Xf+VumSYEdKrXZMF h+W8A7ZNA0r7urEzRNd+wWd4oQW0CvsPjIu7G5XuhSu1Q4Ui+4FVD+tt1hQLVwLxzcjKFh V6MtCq701h7ptCuH89eXaXJi/kiTnjUWG2FEs4BsMO8qBz2nspWVk99y6rO1uQ== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1795020118; b=BQPuDIHjOmiLXK6K531UxVgtme/0ItrXaKSq6E53pD+Y4CXJuzhti26yWCweN2hJezZ/FF PRrDORWXi1kBUYrcnyPIKEk20MaQ0nHVaBSY7IWmrX0hWT5etnqNgWk+ZbZHRX/ZFTUHgM /r/qsMUqfqj2byHg4djuQvqsGpbU/qgGV5OxRLyYYpkgJdGX/f46ID3PDFunWZQ59X33xY TKs3mnG/dFIx3zrTfvZlKuFHN0v4bFnKZbWjDR0imznNiTaFyjxYXZigxDHN428OD9Ljum k5OsDUIv43JQNTjGeNqL3de9lZGvAaLOu3prM8sd6YtR3c6kMOq5v4mRqZr8zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795020118; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Rm9JqdRnwh09ZaYgOmxmZYJ6xE83Shr9CrwBXMfp0PI=; b=avjDbtIjR/ReobCANGErKQH+mYjXrknlYnWMR3qOgad0Hu5PHEpMaz8WbvGBaUr53fD2M4 GLiUkEVLHN6SZB/ro8iCs+nowmxkJgZzvKoTs5bySUYAUR4cHBpmqsr/1fvMjNFfl6jYXc 9LFUlsec2Xe+4gabPApRNPh/9XCOxg5EW4HeQi8ZZdzPFMm9zfvyYs82Odl5jONPXM6HSk yDtjTXslJl3ye2W1V03j6tYCuFEpAFMLF5ygOlUCTDVVXeFgWXzQmrkV3zcrFzXj1ab/4C ic8xJHMnnhW7Sm+WbD0ay6PWHUYRCjuRpPl0+Tj7ou3BNQBbTXs95XY/KX53LA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jV6nf2whNzLBV for ; Wed, 18 Nov 2026 16:41:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 5f84 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Wed, 18 Nov 2026 16:41:58 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Devin Teske Subject: git: b5e5c857e9b4 - main - sysrc: Add .= operator for STRAPPEND List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dteske X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: b5e5c857e9b4d99fb2af9375942e02ecf78ad416 Auto-Submitted: auto-generated Date: Wed, 18 Nov 2026 16:41:58 +0000 Message-Id: <6afdd556.5f84.7150442a@gitrepo.freebsd.org> The branch main has been updated by dteske: URL: https://cgit.FreeBSD.org/src/commit/?id=b5e5c857e9b4d99fb2af9375942e02ecf78ad416 commit b5e5c857e9b4d99fb2af9375942e02ecf78ad416 Author: Devin Teske AuthorDate: 2026-11-18 16:38:06 +0000 Commit: Devin Teske 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