diff options
author | 2024-06-06 01:23:54 +0100 | |
---|---|---|
committer | 2024-06-12 08:06:42 +0100 | |
commit | a46a94768d76cd1f52cc7d6743575887fa416407 (patch) | |
tree | add996fa83a697a76a5430cf495dfafe733b7974 | |
parent | Reposition the declaration of quote_args() (diff) | |
download | gentoo-functions-a46a94768d76cd1f52cc7d6743575887fa416407.tar.gz gentoo-functions-a46a94768d76cd1f52cc7d6743575887fa416407.tar.bz2 gentoo-functions-a46a94768d76cd1f52cc7d6743575887fa416407.zip |
Have quote_args() respect POSIXLY_CORRECT for Issue 7 conformance
Also, markedly improve the comment that documents the function.
Signed-off-by: Kerin Millar <kfm@plushkava.net>
-rw-r--r-- | functions.sh | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/functions.sh b/functions.sh index f440dab..79cd06a 100644 --- a/functions.sh +++ b/functions.sh @@ -540,19 +540,29 @@ parallel_run() } # -# Prints the positional parameters in a manner that approximates the behaviour -# of the ${*@Q} expansion in bash. The output shall be POSIX sh compatible as of -# Issue 8. This should probably be made to exist as a standalone awk script. +# Prints the positional parameters in a format that may be reused as shell +# input. For each considered, it shall be determined whether its value contains +# any non-printable characters in lieu of the US-ASCII character set. If no such +# characters are found, the value shall have each instance of <apostrophe> be +# replaced by <apostrophe><backslash><apostrophe><apostrophe> before being +# enclosed by a pair of <apostrophe> characters. Otherwise, non-printable +# characters shall be replaced by octal escape sequences, <apostrophe> by +# <backslash><apostrophe> and <backslash> by <backslash><backslash>, prior to +# the value being given a prefix of <dollar-sign><apostrophe> and a suffix of +# <apostrophe>, per Issue 8. Finally, the resulting values shall be printed as +# <space> separated. The latter quoting strategy can be suppressed by setting +# the POSIXLY_CORRECT variable as non-empty in the environment. # quote_args() { awk -v q=\' -f - -- "$@" <<-'EOF' BEGIN { + strictly_posix = length(ENVIRON["POSIXLY_CORRECT"]) argc = ARGC ARGC = 1 for (arg_idx = 1; arg_idx < argc; arg_idx++) { arg = ARGV[arg_idx] - if (arg !~ /[\001-\037\177]/) { + if (strictly_posix || arg !~ /[\001-\037\177]/) { gsub(q, q "\\" q q, arg) word = q arg q } else { |