git: 9b4d820f1ead - main - sysrc: Fix -e output

From: Devin Teske <dteske_at_FreeBSD.org>
Date: Wed, 25 Nov 2026 21:51:27 UTC
The branch main has been updated by dteske:

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

commit 9b4d820f1ead5f6a0f65de346d5353bc83fd79b9
Author:     Devin Teske <dteske@FreeBSD.org>
AuthorDate: 2026-11-25 21:46:00 +0000
Commit:     Devin Teske <dteske@FreeBSD.org>
CommitDate: 2026-11-25 21:47:37 +0000

    sysrc: Fix -e output
    
    The sysrc(8) man-page states that `-e' is used to "print query results
    as sh(1) compatible syntax".
    
    Flags such as `-v' when combined with `-e' could make the output
    incompatible with sh(1).
    
    This is a breaking change that should not be MFC'd because it both
    alters certain output formats and also fixes a bug that others may
    have relied upon. When changing value of an existing variable, the
    `-e' output would produce syntax that would revert the change:
    
            $name="$before" # -> $after
    
    This change improves the output of `-e' to:
    
            _$name="$before" $name="$after"
    
    Meanwhile, the output of `-v -e' _queries_ are changed from:
    
            : $file; $name="$value"
    
    to a more useful syntax:
    
            _file_$name="$file" $name="$value"
    
    Further, the output of `-v -e' _assignments_ are changed from:
    
            $file: $name="$before" # -> $after
    
    to the more useful:
    
            _file_$name="$file" _$name="$before" $name="$after"
    
    The sh(1) compatible output now intelligently quotes values so that
    special characters in either filenames or values cannot break sh(1)
    parsing. Values are shown unquoted, double-quoted, or single-quoted
    depending on contents.
    
    In addition:
    
    `-d' now respects `-e' to produces sh(1) compatible syntax.
    `-d' now respects `-n' to omit variable name.
    `-d' now respects `-N' to omit the description.
    
    Reviewed by:    kfv
    Differential Revision:  https://reviews.freebsd.org/D61738
---
 usr.sbin/sysrc/sysrc      | 166 ++++++++++++++++++++++++++++++----------------
 usr.sbin/sysrc/sysrc.subr |  31 +++++++++
 2 files changed, 141 insertions(+), 56 deletions(-)

diff --git a/usr.sbin/sysrc/sysrc b/usr.sbin/sysrc/sysrc
index 617aff16d9ed..d0d5b55847f8 100644
--- a/usr.sbin/sysrc/sysrc
+++ b/usr.sbin/sysrc/sysrc
@@ -16,7 +16,7 @@ pgm="${0##*/}" # Program basename
 #
 # Version information
 #
-SYSRC_VERSION="8.7 Nov-18,2026"
+SYSRC_VERSION="8.8 Nov-18,2026"
 
 #
 # Options
@@ -41,6 +41,11 @@ SHOW_NAME=1		# -n
 SHOW_VALUE=1		# -N
 VERBOSE=		# -v
 
+#
+# Miscellaneous
+#
+SEP=": "
+
 ############################################################ FUNCTIONS
 
 # warn [$fmt [$opts ...]]
@@ -177,29 +182,49 @@ jail_depend()
 	cat $SYSRC_SHARE/sysrc.subr
 }
 
-# escape $string [$var_to_set]
+# escape_required $string
 #
-# Escape $string contents so that the contents can be properly encapsulated in
-# single-quotes (making for safe evaluation).
+# Returns success if $string contains characters unsafe if not escaped. These
+# are characters that would alter the contents or break a shell interpreter's
+# parsing if simply double-quoted.
 #
-# NB: See `bsdconfig includes -dF escape' for relevant information/discussion.
-# NB: Abridged version of `f_shell_escape()' from bsdconfig(8) `strings.subr'.
+escape_required()
+{
+	case "$1" in
+	*[\'\"\$\`\\]*) return $SUCCESS ;;
+	esac
+	return $FAILURE
+}
+
+# quote_required $string
+#
+# Returns success if $string contains characters unsafe if left unquoted.
 #
-escape()
+quote_required()
 {
-	local __start="$1" __var_to_set="$2" __string=
-	while [ "$__start" ]; do
-		case "$__start" in *\'*)
-			__string="$__string${__start%%\'*}'\\''"
-			__start="${__start#*\'}" continue
-		esac
-		break
-	done
-	__string="$__string$__start"
-	if [ "$__var_to_set" ]; then
-		setvar "$__var_to_set" "$__string"
+	case "$1" in
+	*[!-%+,./0-9:=@A-Z\]\[_a-z{}]*) return $SUCCESS ;;
+	esac
+	return $FAILURE
+}
+
+# shell_fmt $fmt $string
+#
+# $fmt should contain precisely one %s. Depending on the contents of $string,
+# Replace %s in $fmt either with "$string", '$string', or $string.
+#
+shell_fmt()
+{
+	local fmt="$1"
+	local string="$2"
+
+	if escape_required "$string"; then
+		f_shell_escape "$string" string
+		printf "$fmt" "'$string'"
+	elif quote_required "$string"; then
+		printf "$fmt" "\"$string\""
 	else
-		echo "$__string"
+		printf "$fmt" "$string"
 	fi
 }
 
@@ -310,9 +335,9 @@ if [ "$LIST_SERVICE_CONFS" ]; then
 	list="${list# }"
 	if [ $# -eq 0 ]; then
 		if [ "$VERBOSE" ]; then
-			echo rc_conf_files: $list
+			echo "rc_conf_files$SEP$list"
 		elif [ "$SHOW_EQUALS" ]; then
-			echo "rc_conf_files=\"$list\""
+			shell_fmt "rc_conf_files=%s\n" "$list"
 		fi
 	fi
 
@@ -340,10 +365,10 @@ if [ "$LIST_SERVICE_CONFS" ]; then
 			[ "$slist" ] || retval=$?
 		fi
 		if [ "$VERBOSE" ]; then
-			[ "$slist" ] && echo "$service: $slist"
+			[ "$slist" ] && echo "$service$SEP$slist"
 			continue
 		elif [ "$SHOW_EQUALS" ]; then
-			[ "$slist" ] && echo "$service=\"$slist\""
+			[ "$slist" ] && shell_fmt "$service=%s\n" "$slist"
 			continue
 		fi
 		list="$list${slist:+ }$slist"
@@ -475,7 +500,6 @@ fi
 #
 # Process `-e', `-N', and `-n' command-line options
 #
-SEP=': '
 [ "$SHOW_FILE" ] && SHOW_EQUALS=
 [ "$SHOW_NAME" ] || SHOW_EQUALS=
 [ "$VERBOSE" = "0" ] && VERBOSE=
@@ -483,7 +507,6 @@ if [ ! "$SHOW_VALUE" ]; then
 	SHOW_NAME=1
 	SHOW_EQUALS=
 fi
-[ "$SHOW_EQUALS" ] && SEP='="'
 
 #
 # Process `-j jail' and `-R dir' command-line options
@@ -514,17 +537,17 @@ if [ "$JAIL" -o "$ROOTDIR" ]; then
 		$( [ "$SHOW_FILE"  ] && echo \ -F )
 	"
 	if [ "$SERVICE" ]; then
-		escape "$SERVICE" _SERVICE
+		f_shell_escape "$SERVICE" _SERVICE
 		args="$args -s '$_SERVICE'"
 		unset _SERVICE
 	fi
 	if [ "${RC_CONFS+set}" ]; then
-		escape "$RC_CONFS" _RC_CONFS
+		f_shell_escape "$RC_CONFS" _RC_CONFS
 		args="$args -f '$_RC_CONFS'"
 		unset _RC_CONFS
 	fi
 	for arg in "$@"; do
-		escape "$arg" arg
+		f_shell_escape "$arg" arg
 		args="$args '$arg'"
 	done
 
@@ -673,10 +696,21 @@ if [ "$SHOW_ALL" ]; then
 			grep -Ev "^($EXCEPT)$"
 		); do
 			#
-			# If enabled, describe rather than expand value
+			# If `-d' is passed, describe rather than expand value
 			#
 			if [ "$DESCRIBE" ]; then
-				echo "$NAME: $( f_sysrc_desc "$NAME" )"
+				if [ ! "$SHOW_NAME" ]; then
+					echo "$( f_sysrc_desc "$NAME" )"
+				elif [ ! "$SHOW_VALUE" ]; then
+					echo "$NAME"
+				elif [ "$SHOW_EQUALS" ]; then
+					desc=$( f_sysrc_desc "$NAME" )
+					shell_fmt "$NAME=%s\n" "$desc"
+				else
+					echo "$NAME$SEP$(
+						f_sysrc_desc "$NAME"
+					)"
+				fi
 				continue
 			fi
 
@@ -684,7 +718,7 @@ if [ "$SHOW_ALL" ]; then
 			# If `-F' is passed, find it and move on
 			#
 			if [ "$SHOW_FILE" ]; then
-				[ "$SHOW_NAME" ] && echo -n "$NAME: "
+				[ "$SHOW_NAME" ] && echo -n "$NAME$SEP"
 				f_sysrc_find "$NAME"
 				continue
 			fi
@@ -697,8 +731,14 @@ if [ "$SHOW_ALL" ]; then
 				continue
 			fi
 
-			[ "$VERBOSE" ] &&
-				echo -n "$( f_sysrc_find "$NAME" ): "
+			if [ "$VERBOSE" ]; then
+				if [ "$SHOW_EQUALS" ]; then
+					file=$( f_sysrc_find "$NAME" )
+					shell_fmt "_file_$NAME=%s " "$file"
+				else
+					echo -n "$( f_sysrc_find "$NAME" )$SEP"
+				fi
+			fi
 
 			#
 			# If `-N' is passed, simplify the output
@@ -708,9 +748,12 @@ if [ "$SHOW_ALL" ]; then
 				continue
 			fi
 
-			echo "${SHOW_NAME:+$NAME$SEP}$(
-				f_sysrc_get${RAW:+_raw} "$NAME"
-			)${SHOW_EQUALS:+\"}"
+			value=$( f_sysrc_get${RAW:+_raw} "$NAME" )
+			if [ "$SHOW_EQUALS" ]; then
+				shell_fmt "$NAME=%s\n" "$value"
+			else
+				echo "${SHOW_NAME:+$NAME$SEP}$value"
+			fi
 
 		done
 	)
@@ -735,25 +778,17 @@ while [ $# -gt 0 ]; do
 	 *) mode=ASSIGN
 	esac
 
-	[ "$DESCRIBE" ] &&
-		echo "$NAME: $( f_sysrc_desc "$NAME" )"
-
 	case "$1" in
 	*=*)
-		#
-		# Like sysctl(8), if both `-d' AND "name=value" is passed,
-		# first describe (done above), then attempt to set
-		#
-
 		# 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 [ "$SHOW_EQUALS" ]; then
-				echo -n ": $file; "
+				shell_fmt "_file_$NAME=%s " "$file"
 			else
-				echo -n "$file: "
+				echo -n "$file$SEP"
 			fi
 		fi
 
@@ -869,16 +904,22 @@ while [ $# -gt 0 ]; do
 							f_sysrc_get_raw "$NAME"
 						)
 					fi
-					echo -n "${SHOW_NAME:+$NAME$SEP}"
-					echo -n "$before${SHOW_EQUALS:+\" #}"
-					echo -n " -> ${SHOW_EQUALS:+\"}$after"
-					echo "${SHOW_EQUALS:+\"}"
+					if [ "$SHOW_EQUALS" ]; then
+						shell_fmt "_$NAME=%s " "$before"
+						shell_fmt "$NAME=%s\n" "$after"
+					else
+						if [ "$SHOW_NAME" ]; then
+							echo -n "$NAME$SEP"
+						fi
+						echo "$before -> $after"
+					fi
 				fi
 			else
 				status=$FAILURE
 			fi
 		fi
 		;;
+
 	*)
 		if ! IGNORED=$( f_sysrc_get "$NAME?" ); then
 			[ "$IGNORE_UNKNOWNS" -o "$QUIET" ] ||
@@ -897,10 +938,19 @@ while [ $# -gt 0 ]; do
 
 		#
 		# Like sysctl(8), when `-d' is passed, desribe it
-		# (already done above) rather than expanding it
+		# rather than expanding it
 		#
-
 		if [ "$DESCRIBE" ]; then
+			if [ ! "$SHOW_NAME" ]; then
+				echo "$( f_sysrc_desc "$NAME" )"
+			elif [ ! "$SHOW_VALUE" ]; then
+				echo "$NAME"
+			elif [ "$SHOW_EQUALS" ]; then
+				desc=$( f_sysrc_desc "$NAME" )
+				shell_fmt "$NAME=%s\n" "$desc"
+			else
+				echo "$NAME$SEP$( f_sysrc_desc "$NAME" )"
+			fi
 			shift 1
 			continue
 		fi
@@ -918,7 +968,7 @@ while [ $# -gt 0 ]; do
 		# If `-F' is passed, find it and move on
 		#
 		if [ "$SHOW_FILE" ]; then
-			[ "$SHOW_NAME" ] && echo -n "$NAME: "
+			[ "$SHOW_NAME" ] && echo -n "$NAME$SEP"
 			f_sysrc_find "$NAME"
 			shift 1
 			continue
@@ -926,9 +976,10 @@ while [ $# -gt 0 ]; do
 
 		if [ "$VERBOSE" ]; then
 			if [ "$SHOW_EQUALS" ]; then
-				echo -n ": $( f_sysrc_find "$NAME" ); "
+				file=$( f_sysrc_find "$NAME" )
+				shell_fmt "_file_$NAME=%s " "$file"
 			else
-				echo -n "$( f_sysrc_find "$NAME" ): "
+				echo -n "$( f_sysrc_find "$NAME" )$SEP"
 			fi
 		fi
 
@@ -937,10 +988,13 @@ while [ $# -gt 0 ]; do
 		#
 		if [ ! "$SHOW_VALUE" ]; then
 			echo "$NAME"
+		elif [ "$SHOW_EQUALS" ]; then
+			value=$( f_sysrc_get${RAW:+_raw} "$NAME" )
+			shell_fmt "$NAME=%s\n" "$value"
 		else
 			echo "${SHOW_NAME:+$NAME$SEP}$(
 				f_sysrc_get${RAW:+_raw} "$NAME"
-			)${SHOW_EQUALS:+\"}"
+			)"
 		fi
 	esac
 	shift 1
diff --git a/usr.sbin/sysrc/sysrc.subr b/usr.sbin/sysrc/sysrc.subr
index d7aef76c8745..2e4ab79706f0 100644
--- a/usr.sbin/sysrc/sysrc.subr
+++ b/usr.sbin/sysrc/sysrc.subr
@@ -121,6 +121,34 @@ f_getvar()
 }
 fi
 
+# f_shell_escape $string [$var_to_set]
+#
+# Escape $string contents so that the contents can be properly encapsulated in
+# single-quotes (making for safe evaluation).
+#
+# NB: See `bsdconfig api -dF escape' for relevant information/discussion.
+# NB: Abridged version of `f_shell_escape()' from bsdconfig(8) `strings.subr'.
+#
+if ! f_have f_shell_escape; then
+f_shell_escape()
+{
+	local __start="$1" __var_to_set="$2" __string=
+	while [ "$__start" ]; do
+		case "$__start" in *\'*)
+			__string="$__string${__start%%\'*}'\\''"
+			__start="${__start#*\'}" continue
+		esac
+		break
+	done
+	__string="$__string$__start"
+	if [ "$__var_to_set" ]; then
+		setvar "$__var_to_set" "$__string"
+	else
+		echo "$__string"
+	fi
+}
+fi
+
 # f_eval_catch [-de] [-k $var_to_set] $funcname $utility \
 #	$printf_format [$printf_args ...]
 #
@@ -342,6 +370,9 @@ f_sysrc_get()
 # return the hostname up to (but not including) the first `.' (see sh(1),
 # "Parameter Expansion" for more information on additional formats).
 #
+# This function is a two-parter. Below is the awk(1) portion of the function,
+# afterward is the sh(1) function which utilizes the below awk script.
+#
 f_sysrc_get_raw_awk='
 # Variables that should be defined on the invocation line:
 # 	-v varname="varname"