diff mbox series

[v3] env: Avoid using GNU features in awk

Message ID 20211120084514.v3.1.Ic6fae5a5f882f8c5476e8b4b34650028419cbcde@changeid
State Superseded
Delegated to: Simon Glass
Headers show
Series [v3] env: Avoid using GNU features in awk | expand

Commit Message

Simon Glass Nov. 20, 2021, 3:45 p.m. UTC
GNU has a very useful third argument to match() but this is not supported
in the POSIX awk.

Update the code to cope, so that the script is POSIX-compliant.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

Changes in v3:
- Reword commit message and comment to avoid casting aspersions

Changes in v2:
- Fix the has_var match() that had left-over test code

 scripts/env2string.awk | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

Comments

Tom Rini Nov. 20, 2021, 3:55 p.m. UTC | #1
On Sat, Nov 20, 2021 at 08:45:34AM -0700, Simon Glass wrote:

> GNU has a very useful third argument to match() but this is not supported
> in the POSIX awk.
> 
> Update the code to cope, so that the script is POSIX-compliant.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Thanks for rewording.

Reviewed-by: Tom Rini <trini@konsulko.com>
Simon Glass Nov. 25, 2021, 12:13 a.m. UTC | #2
Hi Tom,

On Sat, 20 Nov 2021 at 08:55, Tom Rini <trini@konsulko.com> wrote:
>
> On Sat, Nov 20, 2021 at 08:45:34AM -0700, Simon Glass wrote:
>
> > GNU has a very useful third argument to match() but this is not supported
> > in the POSIX awk.
> >
> > Update the code to cope, so that the script is POSIX-compliant.
> >
> > Signed-off-by: Simon Glass <sjg@chromium.org>
>
> Thanks for rewording.
>
> Reviewed-by: Tom Rini <trini@konsulko.com>

Sadly I found another problem on a different machine and had to send v4.

I suppose we'll only know for sure when it lands in master in a month or so.

Regards,
Simon
diff mbox series

Patch

diff --git a/scripts/env2string.awk b/scripts/env2string.awk
index 57d0fc8f3ba..e71c46dfb46 100644
--- a/scripts/env2string.awk
+++ b/scripts/env2string.awk
@@ -24,26 +24,33 @@  NF {
 	# Quote quotes
 	gsub("\"", "\\\"")
 
+	# Avoid using the non-POSIX third parameter to match(), by splitting
+	# the work into several steps.
+	has_var = match($0, "^([^ \t=][^ =]*)=(.*)$")
+
 	# Is this the start of a new environment variable?
-	if (match($0, "^([^ \t=][^ =]*)=(.*)$", arr)) {
+	if (has_var) {
 		if (length(env) != 0) {
 			# Record the value of the variable now completed
 			vars[var] = env
 		}
-		var = arr[1]
-		env = arr[2]
+
+		# Collect the variable name. The value follows the '='
+		match($0, "^([^ \t=][^ =]*)=")
+		var = substr($0, 1, RLENGTH - 1)
+		env = substr($0, RLENGTH + 1)
 
 		# Deal with += which concatenates the new string to the existing
-		# variable
-		if (length(env) != 0 && match(var, "^(.*)[+]$", var_arr))
-		{
+		# variable. Again we are careful to use POSIX match()
+		if (length(env) != 0 && match(var, "^(.*)[+]$")) {
+			plusname = substr(var, RSTART, RLENGTH - 1)
 			# Allow var\+=val to indicate that the variable name is
 			# var+ and this is not actually a concatenation
-			if (substr(var_arr[1], length(var_arr[1])) == "\\") {
+			if (substr(plusname, length(plusname)) == "\\") {
 				# Drop the backslash
 				sub(/\\[+]$/, "+", var)
 			} else {
-				var = var_arr[1]
+				var = plusname
 				env = vars[var] env
 			}
 		}