git: 09768aa53ba3 - main - sysrc: Add -r raw and fixup -c assignment check

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

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

commit 09768aa53ba34ae2101f72cd583eb1f5da051a4e
Author:     Devin Teske <dteske@FreeBSD.org>
AuthorDate: 2026-11-18 16:35:37 +0000
Commit:     Devin Teske <dteske@FreeBSD.org>
CommitDate: 2026-11-18 16:35:37 +0000

    sysrc: Add -r raw and fixup -c assignment check
    
    Add new -r flag to query raw, unexpanded values and display/use raw
    values during assignments (including +=/-= assignments).
    
    Constraint -c assignment checks to always use raw values when testing
    if an assignment would induce a change or if the existing value is
    already set as desired.
    
    PR:             299413
    MFC after:      2 weeks
    Reviewed by:    kfv
    Differential Revision:  https://reviews.freebsd.org/D61584
---
 usr.sbin/sysrc/sysrc               |  40 ++-
 usr.sbin/sysrc/sysrc.8             |  10 +-
 usr.sbin/sysrc/sysrc.subr          | 482 +++++++++++++++++++++++++++++++++++++
 usr.sbin/sysrc/tests/sysrc_test.sh | 102 ++++++++
 4 files changed, 621 insertions(+), 13 deletions(-)

diff --git a/usr.sbin/sysrc/sysrc b/usr.sbin/sysrc/sysrc
index 62faa5120c81..efbe906d9fdd 100644
--- a/usr.sbin/sysrc/sysrc
+++ b/usr.sbin/sysrc/sysrc
@@ -16,7 +16,7 @@ pgm="${0##*/}" # Program basename
 #
 # Version information
 #
-SYSRC_VERSION="7.3.1 Oct-17,2026"
+SYSRC_VERSION="7.4 Nov-14,2026"
 
 #
 # Options
@@ -31,6 +31,7 @@ JAIL=
 LIST_SERVICE_CONFS=
 LIST_CONFS=
 QUIET=
+RAW=
 ROOTDIR=
 SERVICE=
 SHOW_ALL=
@@ -125,12 +126,14 @@ help()
 	      "Show only variable names, not their values."
 	f_err "$optfmt" "-q" \
 	      "Quiet. Disable verbose and hide certain errors."
+	f_err "$optfmt" "-r" \
+	      "Show raw, unexpanded value(s)."
+	f_err "$optfmt" "-R dir" \
+	      "Operate within the root directory \`dir' rather than \`/'."
 	f_err "$optfmt" "-s name" \
 	      "Process additional \`rc.conf.d' entries for service name."
 	f_err "$optfmt" "" \
 	      "Ignored if \`-f file' is given."
-	f_err "$optfmt" "-R dir" \
-	      "Operate within the root directory \`dir' rather than \`/'."
 	f_err "$optfmt" "-v" \
 	      "Verbose. Print the pathname of the specific rc.conf(5)"
 	f_err "$optfmt" "" \
@@ -216,7 +219,7 @@ unset arg
 #
 # Process command-line flags
 #
-while getopts aAcdDeEf:Fhij:lLnNqR:s:vxX flag; do
+while getopts aAcdDeEf:Fhij:lLnNqrR:s:vxX flag; do
 	case "$flag" in
 	a) SHOW_ALL=${SHOW_ALL:-1} ;;
 	A) SHOW_ALL=2 ;;
@@ -237,6 +240,7 @@ while getopts aAcdDeEf:Fhij:lLnNqR:s:vxX flag; do
 	n) SHOW_NAME= ;;
 	N) SHOW_VALUE= ;;
 	q) QUIET=1 VERBOSE= ;;
+	r) RAW=1 ;;
 	R) [ "$OPTARG" ] ||
 		die "%s: Missing or null argument to \`-R' flag" "$pgm"
 	   ROOTDIR="$OPTARG" ;;
@@ -451,6 +455,7 @@ if [ "$JAIL" -o "$ROOTDIR" ]; then
 	args="
 		${VERBOSE:+-v}
 		${QUIET:+-q}
+		${RAW:+-r}
 		$( [ "$DELETE" = "1" ] && echo \ -x )
 		$( [ "$DELETE" = "2" ] && echo \ -X )
 		$( [ "$SHOW_ALL" = "1" ] && echo \ -a )
@@ -578,8 +583,9 @@ if [ "$SHOW_ALL" ]; then
 		EXCEPT="$EXCEPT|DELETE|SHOW_ALL|SHOW_EQUALS|SHOW_NAME|DEFAULT"
 		EXCEPT="$EXCEPT|SHOW_VALUE|SHOW_FILE|VERBOSE|RC_CONFS|SERVICE"
 		EXCEPT="$EXCEPT|pgm|SUCCESS|FAILURE|CHECK_ONLY|EXISTING_ONLY"
-		EXCEPT="$EXCEPT|LIST_CONFS|LIST_SERVICE_CONFS"
+		EXCEPT="$EXCEPT|LIST_CONFS|LIST_SERVICE_CONFS|RAW"
 		EXCEPT="$EXCEPT|f_sysrc_desc_awk|f_sysrc_delete_awk"
+		EXCEPT="$EXCEPT|f_sysrc_get_raw_awk"
 
 		#
 		# Clean the environment (except for our required variables)
@@ -662,7 +668,8 @@ if [ "$SHOW_ALL" ]; then
 			fi
 
 			echo "${SHOW_NAME:+$NAME$SEP}$(
-			      f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
+				f_sysrc_get${RAW:+_raw} "$NAME"
+			)${SHOW_EQUALS:+\"}"
 
 		done
 	)
@@ -723,7 +730,11 @@ while [ $# -gt 0 ]; do
 		#
 		case "$mode" in
 		APPEND)
-			before=$( f_sysrc_get "$NAME" )
+			if [ "$CHECK_ONLY$RAW" ]; then
+				before=$( f_sysrc_get_raw "$NAME" )
+			else
+				before=$( f_sysrc_get "$NAME" )
+			fi
 			add="${1#*=}"
 			delim="${add%"${add#?}"}" # first character
 			oldIFS="$IFS"
@@ -745,7 +756,11 @@ while [ $# -gt 0 ]; do
 			[ "$SHOW_FILE" ] && before=$( f_sysrc_find "$NAME" )
 			;;
 		REMOVE)
-			before=$( f_sysrc_get "$NAME" )
+			if [ "$CHECK_ONLY$RAW" ]; then
+				before=$( f_sysrc_get_raw "$NAME" )
+			else
+				before=$( f_sysrc_get "$NAME" )
+			fi
 			remove="${1#*=}"
 			delim="${remove%"${remove#?}"}" # first character
 			oldIFS="$IFS"
@@ -769,6 +784,8 @@ while [ $# -gt 0 ]; do
 		*) # ASSIGN
 			if [ "$SHOW_FILE" ]; then
 				before=$( f_sysrc_find "$NAME" )
+			elif [ "$CHECK_ONLY$RAW" ]; then
+				before=$( f_sysrc_get_raw "$NAME" )
 			else
 				before=$( f_sysrc_get "$NAME" )
 			fi
@@ -813,6 +830,10 @@ while [ $# -gt 0 ]; do
 				if [ ! "$QUIET" ]; then
 					if [ "$SHOW_FILE" ]; then
 						after=$( f_sysrc_find "$NAME" )
+					elif [ "$RAW" ]; then
+						after=$(
+							f_sysrc_get_raw "$NAME"
+						)
 					else
 						after=$( f_sysrc_get "$NAME" )
 					fi
@@ -886,7 +907,8 @@ while [ $# -gt 0 ]; do
 			echo "$NAME"
 		else
 			echo "${SHOW_NAME:+$NAME$SEP}$(
-			      f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
+				f_sysrc_get${RAW:+_raw} "$NAME"
+			)${SHOW_EQUALS:+\"}"
 		fi
 	esac
 	shift 1
diff --git a/usr.sbin/sysrc/sysrc.8 b/usr.sbin/sysrc/sysrc.8
index cb32f72ea587..2cadeb8f03ca 100644
--- a/usr.sbin/sysrc/sysrc.8
+++ b/usr.sbin/sysrc/sysrc.8
@@ -1,7 +1,7 @@
 .\"
 .\" SPDX-License-Identifier: BSD-2-Clause
 .\"
-.\" Copyright (c) 2011-2016 Devin Teske
+.\" Copyright (c) 2011-2026 Devin Teske
 .\" All rights reserved.
 .\"
 .\" Redistribution and use in source and binary forms, with or without
@@ -25,7 +25,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd April 3, 2022
+.Dd November 15, 2026
 .Dt SYSRC 8
 .Os
 .Sh NAME
@@ -33,14 +33,14 @@
 .Nd safely edit system rc files
 .Sh SYNOPSIS
 .Nm
-.Op Fl cdDeEFhinNqvx
+.Op Fl cdDeEFhinNqrvx
 .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 ...
 .Nm
-.Op Fl cdDeEFhinNqvx
+.Op Fl cdDeEFhinNqrvx
 .Op Fl s Ar name
 .Op Fl f Ar file
 .Op Fl j Ar jail | Fl R Ar dir
@@ -147,6 +147,8 @@ When combined with
 and one or more
 .Ar name
 arguments, provide only exit status and no output.
+.It Fl r
+Show raw, unexpanded value(s).
 .It Fl R Ar dir
 Operate within the root directory
 .Sq Ar dir
diff --git a/usr.sbin/sysrc/sysrc.subr b/usr.sbin/sysrc/sysrc.subr
index 0396628c929f..d7aef76c8745 100644
--- a/usr.sbin/sysrc/sysrc.subr
+++ b/usr.sbin/sysrc/sysrc.subr
@@ -294,6 +294,488 @@ f_sysrc_get()
 	)
 }
 
+# f_sysrc_get_raw $varname
+#
+# Without expanding the value (e.g., in the case of `-c' check), get a system
+# configuration setting from the collection of system-configuration files (in
+# order: /etc/defaults/rc.conf /etc/rc.conf and /etc/rc.conf.local)
+#
+# Much of the complexity here is to handle things like:
+#
+# 1. Even-vs-Odd backslash count
+# 	a. preceding things like ", ', $, `, NL, etc.
+# 2. Single-vs-Double quoting
+# 3. Dollar quoting
+# 	a. $'\t' to expand escapes (in example, a real tab)
+# 	b. $"File not found." i18n translation; for example:
+# 	   export LC_ALL=es_ES.UTF-8
+# 	   export TEXTDOMAIN=bash
+# 	   export TEXTDOMAINDIR=/usr/local/share/locale
+# 	   bash -c 'echo $"%s: not found"' -> %s: no encontrado
+# 	NB: /bin/sh has no *.mo files but the syntax should still be supported
+# 4. Compound quoting
+# 	a. single-with-double or vice-versa ["a"'b' or 'c'"d"]
+# 	b. bare-with-quoted [abc\ '123' or "def"\ 456]
+# 	c. joined arithmetic ["foo"$(( 1 + 1 ))]
+# 	d. joined command substitutions ["bar"`baz` or "bar"$( baz )]
+# 5. Parameter expansions
+# 	a. Without quotes [${abc#123}]
+# 	b. With quotes [${foo#"bar"} or ${bar#'baz'}]
+# 	c. Quoted with same quotes ["${foo#"bar"}"]
+# 	d. Single-quoted literal compound ['${foo#'bar'}']
+# 	e. With whitespace [${foo#bar baz}]
+# 	f. Nested [${foo#`bar ${baz}`}]
+# 6. Newlines and line continuations
+# 	a. Inside single-quotes
+# 	b. Inside double-quotes
+# 	c. Unquoted using a trailing backslash at the end of the line
+# 7. Multi-assignment lines [foo=bar bar=baz]
+# 	a. With various flavors of quoting or joined statements
+# 	   [foo="abc"`def 123`${ghi#"456"} bar=baz]
+# 	   [foo=abc;bar=baz]
+# 	NB: Only works to return correct value if requested variable
+# 	    is the first word assignment (e.g., asking for foo).
+# 8. Combinations and mixtures of the above
+#
+# NOTE: Additional shell parameter-expansion formats are supported. For
+# example, passing an argument of "hostname%%.*" (properly quoted) will
+# return the hostname up to (but not including) the first `.' (see sh(1),
+# "Parameter Expansion" for more information on additional formats).
+#
+f_sysrc_get_raw_awk='
+# Variables that should be defined on the invocation line:
+# 	-v varname="varname"
+#
+BEGIN {
+	# NB: Should be same regex as f_sysrc_find()
+	regex = sprintf("^[[:space:]]*%s=", varname)
+	value = ""
+}
+
+#
+# Quote-remove one assignment word starting at start
+# NB: Supports varname=value othervar=othervalue ...
+#
+function read_value(start,    out, q, ch)
+{
+	q_i = start
+	q_at_nl = 0
+	sq = "\047"
+	out = ""
+	q = 0
+	while (next_char()) {
+		ch = q_c
+		if ( q == 1 ) {
+			if ( ch == sq ) {
+				q = 0
+				continue
+			}
+			out = out ch
+			continue
+		}
+		if ( q == 2 ) {
+			if ( ch == "\\" ) {
+				if ( !next_char() ) {
+					out = out "\\"
+					break
+				}
+				ch = q_c
+				if ( ch == "\n" ) continue
+				if ( ch == "$" || ch == "`" ||
+				     ch == "\"" || ch == "\\" )
+					out = out ch
+				else
+					out = out "\\" ch
+				continue
+			}
+			if ( ch == "\"" ) {
+				q = 0
+				continue
+			}
+			if ( ch == "$" ) {
+				out = out read_dollar(1)
+				continue
+			}
+			if ( ch == "`" ) {
+				out = out read_bquote()
+				continue
+			}
+			out = out ch
+			continue
+		}
+		if ( ch == sq ) {
+			q = 1
+			continue
+		}
+		if ( ch == "\"" ) {
+			q = 2
+			continue
+		}
+		if ( ch == "\\" ) {
+			if ( !next_char() ) {
+				out = out "\\"
+				break
+			}
+			if ( q_c == "\n" ) continue
+			out = out q_c
+			continue
+		}
+		if ( word_end(ch) ) break
+		if ( ch == "$" ) {
+			out = out read_dollar()
+			continue
+		}
+		if ( ch == "`" ) {
+			out = out read_bquote()
+			continue
+		}
+		out = out ch
+	}
+	return out
+}
+
+function word_end(ch)
+{
+	if ( ch == "\n" || ch == " " || ch == "\t" ) return 1
+	if ( ch == ";" || ch == "&" || ch == "|" ) return 1
+	if ( ch == "<" || ch == ">" || ch == "(" || ch == ")" ) return 1
+	return 0
+}
+
+function next_char(    r)
+{
+	if ( q_i <= length($0) ) {
+		q_c = substr($0, q_i, 1)
+		q_i++
+		return 1
+	}
+	if ( q_at_nl ) {
+		r = getline
+		if ( r <= 0 ) return 0
+		q_at_nl = 0
+		if ( length($0) == 0 ) {
+			q_c = "\n"
+			q_at_nl = 1
+			q_i = 1
+			return 1
+		}
+		q_c = substr($0, 1, 1)
+		q_i = 2
+		return 1
+	}
+	q_c = "\n"
+	q_at_nl = 1
+	return 1
+}
+
+function peek_char()
+{
+	if ( q_i <= length($0) ) return substr($0, q_i, 1)
+	if ( q_at_nl ) return ""
+	return "\n"
+}
+
+# Copy one expansion without running it. inq means the $ is already
+# inside double quotes; a dollar quote is kept whole only when unquoted.
+function read_dollar(inq,    p)
+{
+	p = peek_char()
+	if ( p == "(" ) {
+		next_char()
+		if ( peek_char() == "(" ) {
+			next_char()
+			return read_arith()
+		}
+		return read_cmd()
+	}
+	if ( p == "{" ) {
+		next_char()
+		return read_param()
+	}
+	if ( !inq && (p == sq || p == "\"") )
+		return read_dolquote()
+	return "$"
+}
+
+# Dollar-single or dollar-double quote; marks kept, escapes left as written
+function read_dolquote(    out, qch, ch)
+{
+	qch = peek_char()
+	next_char()
+	out = "$" qch
+	while (next_char()) {
+		ch = q_c
+		if ( ch == "\\" ) {
+			out = out ch
+			if ( !next_char() ) break
+			out = out q_c
+			continue
+		}
+		out = out ch
+		if ( ch == qch ) break
+	}
+	return out
+}
+
+function read_cmd(    out, depth, q, ch)
+{
+	out = "$("
+	depth = 1
+	q = 0
+	while (depth > 0 && next_char()) {
+		ch = q_c
+		if ( q == 1 ) {
+			out = out ch
+			if ( ch == sq ) q = 0
+			continue
+		}
+		if ( q == 2 ) {
+			if ( ch == "\\" ) {
+				out = out ch
+				if ( !next_char() ) break
+				out = out q_c
+				continue
+			}
+			out = out ch
+			if ( ch == "\"" ) q = 0
+			continue
+		}
+		if ( ch == sq ) {
+			out = out ch
+			q = 1
+			continue
+		}
+		if ( ch == "\"" ) {
+			out = out ch
+			q = 2
+			continue
+		}
+		if ( ch == "\\" ) {
+			out = out ch
+			if ( !next_char() ) break
+			out = out q_c
+			continue
+		}
+		if ( ch == "$" ) {
+			out = out read_dollar()
+			continue
+		}
+		if ( ch == "`" ) {
+			out = out read_bquote()
+			continue
+		}
+		if ( ch == "(" ) depth++
+		else if ( ch == ")" ) depth--
+		out = out ch
+	}
+	return out
+}
+
+function read_param(    out, q, ch)
+{
+	out = "${"
+	q = 0
+	while (next_char()) {
+		ch = q_c
+		if ( q == 1 ) {
+			out = out ch
+			if ( ch == sq ) q = 0
+			continue
+		}
+		if ( q == 2 ) {
+			if ( ch == "\\" ) {
+				out = out ch
+				if ( !next_char() ) break
+				out = out q_c
+				continue
+			}
+			out = out ch
+			if ( ch == "\"" ) q = 0
+			continue
+		}
+		if ( ch == sq ) {
+			out = out ch
+			q = 1
+			continue
+		}
+		if ( ch == "\"" ) {
+			out = out ch
+			q = 2
+			continue
+		}
+		if ( ch == "\\" ) {
+			out = out ch
+			if ( !next_char() ) break
+			out = out q_c
+			continue
+		}
+		if ( ch == "$" ) {
+			out = out read_dollar()
+			continue
+		}
+		if ( ch == "`" ) {
+			out = out read_bquote()
+			continue
+		}
+		out = out ch
+		if ( ch == "}" ) break
+	}
+	return out
+}
+
+function read_arith(    out, depth, ch)
+{
+	out = "$(("
+	depth = 0
+	while (next_char()) {
+		ch = q_c
+		if ( ch == "$" ) {
+			out = out read_dollar()
+			continue
+		}
+		if ( ch == "`" ) {
+			out = out read_bquote()
+			continue
+		}
+		if ( ch == "\\" ) {
+			out = out ch
+			if ( !next_char() ) break
+			out = out q_c
+			continue
+		}
+		if ( ch == "(" ) {
+			depth++
+			out = out ch
+			continue
+		}
+		if ( ch == ")" ) {
+			if ( depth > 0 ) {
+				depth--
+				out = out ch
+				continue
+			}
+			out = out ch
+			if ( peek_char() == ")" ) {
+				next_char()
+				out = out q_c
+				break
+			}
+			continue
+		}
+		out = out ch
+	}
+	return out
+}
+
+function read_bquote(    out, q, ch, p)
+{
+	out = "`"
+	q = 0
+	while (next_char()) {
+		ch = q_c
+		if ( q == 1 ) {
+			out = out ch
+			if ( ch == sq ) q = 0
+			continue
+		}
+		if ( q == 2 ) {
+			if ( ch == "\\" ) {
+				out = out ch
+				if ( !next_char() ) break
+				out = out q_c
+				continue
+			}
+			out = out ch
+			if ( ch == "\"" ) q = 0
+			continue
+		}
+		if ( ch == sq ) {
+			out = out ch
+			q = 1
+			continue
+		}
+		if ( ch == "\"" ) {
+			out = out ch
+			q = 2
+			continue
+		}
+		if ( ch == "\\" ) {
+			p = peek_char()
+			out = out ch
+			if ( p == "$" || p == "`" ||
+			     p == "\\" || p == "\n" ) {
+				if ( !next_char() ) break
+				out = out q_c
+			}
+			continue
+		}
+		if ( ch == "`" ) {
+			out = out ch
+			break
+		}
+		if ( ch == "$" ) {
+			out = out read_dollar()
+			continue
+		}
+		out = out ch
+	}
+	return out
+}
+
+/^[[:space:]]*(#|$)/ { next } # skip blank lines and comments
+match($0, regex) { value = read_value(RSTART + RLENGTH) }
+
+END { printf "%s\n", value }
+'
+f_sysrc_get_raw()
+{
+	local varname="${1%%[!$VALID_VARNAME_CHARS]*}"
+	local paramexp="${1#"$varname"}"
+	local file value
+
+	# Taint-check variable name
+	case "$varname" in
+	[0-9]*)
+		# Don't expand possible positional parameters
+		return $FAILURE ;;
+	*)
+		[ "$varname" ] || return $FAILURE
+	esac
+
+	if ! file=$( f_sysrc_find "$varname" ); then
+		#
+		# Like f_sysrc_get() given an unset variable, return no value
+		# with success unless paramexp specifically requests error
+		#
+		case "$paramexp" in
+		"?"*)
+			# Same exit status as: f_sysrc_get 'varname?'
+			return 2 ;;
+		esac
+		echo
+		return $SUCCESS
+	fi
+
+	#
+	# To get the raw value, we cannot simply source the file,
+	# we must instead read the file
+	#
+	value=$( awk -v varname="$varname" "$f_sysrc_get_raw_awk" "$file" )
+
+	#
+	# This must be the last functional line for the function to preserve
+	# the return status from formats such as "varname:?" (see "Parameter
+	# Expansion" in sh(1) for more information).
+	#
+	# NB: The parameter expansion "varname?" cannot work here because if
+	# f_sysrc_find() did not find a setting, we have not reached this point
+	# but instead returned error status. So if we reach here, that means we
+	# already know the variable is set. Parameter expansion "varname:?"
+	# does still have meaning to test that the value is non-NULL.
+	#
+	eval printf "'%s\\n'" '"${value'"$paramexp"'}"' 2> /dev/null
+}
+
 # f_sysrc_service_configs [-a|-p] $name [$var_to_set]
 #
 # Get a list of optional `rc.conf.d' entries sourced by system `rc.d' script
diff --git a/usr.sbin/sysrc/tests/sysrc_test.sh b/usr.sbin/sysrc/tests/sysrc_test.sh
index fa79e6262c21..d284207313bc 100644
--- a/usr.sbin/sysrc/tests/sysrc_test.sh
+++ b/usr.sbin/sysrc/tests/sysrc_test.sh
@@ -262,6 +262,51 @@ c_flag_body()
 	    sysrc -f "${SYSRC_CONF}" -c test_enable=YES test_list-=item1
 }
 
+atf_test_case c_flag_quoting
+c_flag_quoting_head()
+{
+	atf_set "descr" "Verify -c compares raw assignment text"
+}
+c_flag_quoting_body()
+{
+	local msg
+
+	# PR 299413: the expanded value is " -J 15"
+	printf '%s\n' 'cron_flags="$cron_flags -J 15"' > "${SYSRC_CONF}"
+	msg="${SYSRC_CONF}: cron_flags: already set to"
+	msg="$msg \`\$cron_flags -J 15'"
+	atf_check -o inline:"$msg\n" \
+	    sysrc -f "${SYSRC_CONF}" -v -c 'cron_flags=$cron_flags -J 15'
+	msg="${SYSRC_CONF}: cron_flags: would change from"
+	msg="$msg \`\$cron_flags -J 15' to \`\$cron_flags -J 16'"
+	atf_check -s exit:1 -o inline:"$msg\n" \
+	    sysrc -f "${SYSRC_CONF}" -v -c 'cron_flags=$cron_flags -J 16'
+
+	# Same raw text in the other quote forms
+	printf '%s\n' "cron_flags='\$cron_flags -J 15'" > "${SYSRC_CONF}"
+	atf_check sysrc -f "${SYSRC_CONF}" -c 'cron_flags=$cron_flags -J 15'
+	printf '%s\n' 'cron_flags=$cron_flags\ -J\ 15' > "${SYSRC_CONF}"
+	atf_check sysrc -f "${SYSRC_CONF}" -c 'cron_flags=$cron_flags -J 15'
+	printf '%s\n' "cron_flags='\$cron_flags'\" -J 15\"" > "${SYSRC_CONF}"
+	atf_check sysrc -f "${SYSRC_CONF}" -c 'cron_flags=$cron_flags -J 15'
+
+	# Dollar quotes keep their quote marks
+	printf '%s\n' 'cron_flags=$"%s: file not found"' > "${SYSRC_CONF}"
+	msg="${SYSRC_CONF}: cron_flags: already set to"
+	msg="$msg \`\$\"%s: file not found\"'"
+	atf_check -o inline:"$msg\n" \
+	    sysrc -f "${SYSRC_CONF}" -v -c 'cron_flags=$"%s: file not found"'
+	atf_check -s exit:1 \
+	    sysrc -f "${SYSRC_CONF}" -c 'cron_flags=$%s: file not found'
+	printf '%s\n' "cron_flags=\$'a\\tb'" > "${SYSRC_CONF}"
+	msg="${SYSRC_CONF}: cron_flags: already set to"
+	msg="$msg \`\$'a\\\\tb''"
+	atf_check -o inline:"$msg\n" \
+	    sysrc -f "${SYSRC_CONF}" -v -c "cron_flags=\$'a\\tb'"
+	atf_check -s exit:1 \
+	    sysrc -f "${SYSRC_CONF}" -c 'cron_flags=$a\tb'
+}
+
 atf_test_case d_flag cleanup
 d_flag_head()
 {
@@ -334,6 +379,61 @@ F_flag_body()
 	    -f ${sysrc2_conf} -F test_enable test_list
 }
 
+atf_test_case r_flag
+r_flag_head()
+{
+	atf_set "descr" "Verify -r shows raw assignment text"
+}
+r_flag_body()
+{
+	local msg
+
+	# Assignment keeps the expansion text
+	msg="test_flags:  -> \$test_flags -J 15"
+	atf_check -o inline:"$msg\n" \
+	    sysrc -f "${SYSRC_CONF}" -r 'test_flags=$test_flags -J 15'
+	atf_check -o inline:'test_flags="$test_flags -J 15"\n' \
+	    cat "${SYSRC_CONF}"
+
+	# Query expands; -r does not
+	atf_check -o inline:"test_flags:  -J 15\n" \
+	    sysrc -f "${SYSRC_CONF}" test_flags
+	atf_check -o inline:"test_flags: \$test_flags -J 15\n" \
+	    sysrc -f "${SYSRC_CONF}" -r test_flags
+
+	# Replace the raw text
+	msg="test_flags: \$test_flags -J 15 ->"
+	msg="$msg \$test_flags -J 16"
+	atf_check -o inline:"$msg\n" \
+	    sysrc -f "${SYSRC_CONF}" -r 'test_flags=$test_flags -J 16'
+
+	# Append to the raw text
+	msg="test_flags: \$test_flags -J 16 ->"
+	msg="$msg \$test_flags -J 16 extra"
+	atf_check -o inline:"$msg\n" \
+	    sysrc -f "${SYSRC_CONF}" -r test_flags+=extra
+	atf_check -o inline:'test_flags="$test_flags -J 16 extra"\n' \
+	    cat "${SYSRC_CONF}"
+
+	# Appending a token already present does not duplicate it
+	msg="test_flags: \$test_flags -J 16 extra ->"
+	msg="$msg \$test_flags -J 16 extra"
+	atf_check -o inline:"$msg\n" \
+	    sysrc -f "${SYSRC_CONF}" -r test_flags+=extra
+
+	# Subtract a token; the unexpanded name stays
+	msg="test_flags: \$test_flags -J 16 extra ->"
+	msg="$msg \$test_flags -J 16"
+	atf_check -o inline:"$msg\n" \
+	    sysrc -f "${SYSRC_CONF}" -r test_flags-=extra
+
+	# Drop $test_flags. A leading word keeps the space delimiter
+	msg="test_flags: \$test_flags -J 16 -> -J 16"
+	atf_check -o inline:"$msg\n" \
+	    sysrc -f "${SYSRC_CONF}" -r 'test_flags-=x $test_flags'
+	atf_check -o inline:'test_flags="-J 16"\n' cat "${SYSRC_CONF}"
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case check_variable
@@ -345,7 +445,9 @@ atf_init_test_cases()
 	atf_add_test_case a_flag
 	atf_add_test_case A_flag
 	atf_add_test_case c_flag
+	atf_add_test_case c_flag_quoting
 	atf_add_test_case d_flag
 	atf_add_test_case f_flag
 	atf_add_test_case F_flag
+	atf_add_test_case r_flag
 }