git: 82cb2a4158fa - main - Update safe_eval.sh to support --export

From: Simon J. Gerraty <sjg_at_FreeBSD.org>
Date: Thu, 15 Aug 2024 22:44:02 UTC
The branch main has been updated by sjg:

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

commit 82cb2a4158fa79ec230a5ab41a2f61c253e65c0b
Author:     Simon J. Gerraty <sjg@FreeBSD.org>
AuthorDate: 2024-08-15 22:42:39 +0000
Commit:     Simon J. Gerraty <sjg@FreeBSD.org>
CommitDate: 2024-08-15 22:42:39 +0000

    Update safe_eval.sh to support --export
    
    This update allows
    
    safe_dot --export file ...
    
    to export any variables that get set.
    
    Reviewed by: obrien
---
 libexec/rc/safe_eval.sh | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)

diff --git a/libexec/rc/safe_eval.sh b/libexec/rc/safe_eval.sh
index 10b6ed09c769..0e1410b771cb 100644
--- a/libexec/rc/safe_eval.sh
+++ b/libexec/rc/safe_eval.sh
@@ -1,9 +1,9 @@
 # SPDX-License-Identifier: BSD-2-Clause
 
 # RCSid:
-#	$Id: safe_eval.sh,v 1.12 2023/10/12 18:46:53 sjg Exp $
+#	$Id: safe_eval.sh,v 1.16 2024/08/15 02:28:30 sjg Exp $
 #
-#	@(#) Copyright (c) 2023 Simon J. Gerraty
+#	@(#) Copyright (c) 2023-2024 Simon J. Gerraty
 #
 #	This file is provided in the hope that it will
 #	be of use.  There is absolutely NO WARRANTY.
@@ -37,14 +37,33 @@ safe_eval() {
     eval `cat "$@" | safe_set`
 }
 
+##
+# safe_eval_export [file]
+#
+# eval variable assignments only from file
+# taking care to eliminate any shell meta chars
+# export any variables thus set
+#
+safe_eval_export() {
+    eval `cat "$@" | safe_set | ${SED:-sed} 's/^\([^=]*\)=.*/&; export \1/'`
+}
+
 ##
 # safe_dot file [...]
 #
 # feed all "file" that exist to safe_eval
 #
 safe_dot() {
-    local ef= f
-
+    eval ${local:-:} ef ex f
+    ef=
+    ex=
+    while :
+    do
+        case "$1" in
+        --export) ex=_export; shift;;
+        *) break;;
+        esac
+    done
     for f in "$@"
     do
         test -s $f || continue
@@ -52,7 +71,7 @@ safe_dot() {
         dotted="$dotted $f"
     done
     test -z "$ef" && return 1
-    safe_eval $ef
+    safe_eval$ex $ef
     return 0
 }