git: b75bb99621fb - main - rc.subr add Exists so we can find sed

From: Simon J. Gerraty <sjg_at_FreeBSD.org>
Date: Mon, 12 Feb 2024 22:40:30 UTC
The branch main has been updated by sjg:

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

commit b75bb99621fbebae3b7f56ea01e711333d0455d8
Author:     Simon J. Gerraty <sjg@FreeBSD.org>
AuthorDate: 2024-02-12 22:39:20 +0000
Commit:     Simon J. Gerraty <sjg@FreeBSD.org>
CommitDate: 2024-02-12 22:39:20 +0000

    rc.subr add Exists so we can find sed
    
    SED=`Exists -x /usr/bin/sed /rescue/sed`
    
    avoids adding /rescure to $PATH, and allows use of sed
    before /usr is mounted (if a separate filesystem).
    
    Reviewed by:    jlduran_gmail.com
    Differential Revision:  https://reviews.freebsd.org/D43826
---
 libexec/rc/rc.subr      | 40 +++++++++++++++++++++++++++++++++++-----
 libexec/rc/safe_eval.sh |  2 +-
 2 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/libexec/rc/rc.subr b/libexec/rc/rc.subr
index d76f0ba4f9a7..16b2c9fc5e88 100644
--- a/libexec/rc/rc.subr
+++ b/libexec/rc/rc.subr
@@ -156,6 +156,33 @@ vdot()
 	return $rc
 }
 
+# Exists [test] file ...
+# report the first "file" that passes "test" (default -s).
+Exists()
+{
+	local f _t=-s
+
+	while :; do
+		: 1=$1
+		case "$1" in
+		-?)
+			_t=$1
+			shift
+			;;
+		*)
+			break
+			;;
+		esac
+	done
+
+	for f in "$@"; do
+		[ $_t $f ] || continue
+		echo $f
+		return 0
+	done
+	return 1
+}
+
 # do we have $1 (could be a function)
 have()
 {
@@ -179,8 +206,8 @@ rc_trace()
 		[ -f $cf ] || return
 		if [ -s $cf ]; then
 			# don't try to set RC_LEVEL without sed
-			if [ -x /usr/bin/sed ]; then
-				RC_LEVEL=$(sed -n '/^RC_LEVEL=/ { s/.*=//p;q; }' $cf)
+			if [ -n "$SED" ]; then
+				RC_LEVEL=$($SED -n '/^RC_LEVEL=/ { s/.*=//p;q; }' $cf)
 				RC_LEVEL=${RC_LEVEL:-0}
 			fi
 		else
@@ -2498,6 +2525,8 @@ if [ -n "$boottrace_cmd" ] && [ "`${SYSCTL_N} -q kern.boottrace.enabled`" = "1"
 	rc_boottrace=YES
 fi
 
+SED=${SED:-$(Exists -x /usr/bin/sed /rescue/sed)}
+
 # Allow for local additions and overrides.
 # Use vdot to ensure the file has not been tampered with.
 vdot /etc/local.rc.subr
@@ -2514,10 +2543,11 @@ if ! have basename; then
 	{
 		return 0
 	}
-else
-	# safe_eval.sh provides safe_dot - for untrusted files
-	$_SAFE_EVAL_SH vdot /libexec/safe_eval.sh
+	# we cannot use safe_dot without sed
+	[ -z "$SED" ] && _SAFE_EVAL_SH=:
 fi
+# safe_eval.sh provides safe_dot - for untrusted files
+$_SAFE_EVAL_SH vdot /libexec/safe_eval.sh
 $_DEBUG_SH vdot /libexec/debug.sh
 
 # Ensure we can still operate if debug.sh and
diff --git a/libexec/rc/safe_eval.sh b/libexec/rc/safe_eval.sh
index bd9bc9394814..10b6ed09c769 100644
--- a/libexec/rc/safe_eval.sh
+++ b/libexec/rc/safe_eval.sh
@@ -24,7 +24,7 @@ _SAFE_EVAL_SH=:
 # any non-alphanumeric chars are replaced with '_'
 #
 safe_set() {
-    sed 's/[ 	]*#.*//;/^[A-Za-z_][A-Za-z0-9_]*=/!d;s;[^A-Za-z0-9_. 	"$,/=-];_;g'
+    ${SED:-sed} 's/[ 	]*#.*//;/^[A-Za-z_][A-Za-z0-9_]*=/!d;s;[^A-Za-z0-9_. 	"$,/=-];_;g'
 }
 
 ##