From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 74FCF1581D3 for ; Sun, 19 May 2024 15:27:58 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id F3FF6E2A05; Sun, 19 May 2024 15:27:56 +0000 (UTC) Received: from smtp.gentoo.org (dev.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id DAABDE2A05 for ; Sun, 19 May 2024 15:27:56 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id F01EC33BE58 for ; Sun, 19 May 2024 15:27:55 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 8B1591704 for ; Sun, 19 May 2024 15:27:54 +0000 (UTC) From: "Sam James" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" Message-ID: <1716130113.1b936a7427ec2100b782dd2e19b47b7f54886be3.sam@gentoo> Subject: [gentoo-commits] proj/gentoo-functions:master commit in: / X-VCS-Repository: proj/gentoo-functions X-VCS-Files: functions.sh X-VCS-Directories: / X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: 1b936a7427ec2100b782dd2e19b47b7f54886be3 X-VCS-Branch: master Date: Sun, 19 May 2024 15:27:54 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: a7391ea4-545d-450e-afc3-1195f2a83e4b X-Archives-Hash: 6a100fb809f45dc195e526f54d2bf821 commit: 1b936a7427ec2100b782dd2e19b47b7f54886be3 Author: Kerin Millar plushkava net> AuthorDate: Sun May 19 14:33:54 2024 +0000 Commit: Sam James gentoo org> CommitDate: Sun May 19 14:48:33 2024 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=1b936a74 Significantly improve _print_args() behaviour It will now employ $'' style quoting where useful, per Issue 8: https://austingroupbugs.net/view.php?id=249 Characters whose ordinal values are lower than 0x20 will be converted to octal sequences. It should be considered in due course whether this ought to exist as a standalone awk script. Signed-off-by: Kerin Millar plushkava.net> functions.sh | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/functions.sh b/functions.sh index 462db97..890fa7a 100644 --- a/functions.sh +++ b/functions.sh @@ -94,8 +94,8 @@ ebegin() # edo() { genfun_cmd=$(_print_args "$@") - einfo "${genfun_cmd% }" - "$@" || die "Failed to run command: ${genfun_cmd% }" + einfo "${genfun_cmd}" + "$@" || die "Failed to run command: ${genfun_cmd}" } # @@ -528,18 +528,46 @@ _is_visible() # # Prints the positional parameters in a manner that approximates the behaviour -# of the ${*@Q} expansion in bash. +# 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. +# # _print_args() { awk -v q=\' -f - -- "$@" <<-'EOF' BEGIN { + for (i = 1; i < 32; i++) { + char = sprintf("%c", i) + ord_by[char] = i + } argc = ARGC ARGC = 1 - for (i = 1; i < argc; i++) { - arg = ARGV[i] - gsub(q, q "\\" q q, arg) - printf("'%s' ", arg) + for (arg_idx = 1; arg_idx < argc; arg_idx++) { + arg = ARGV[arg_idx] + if (arg !~ /[\001-\037]/) { + gsub(q, q "\\" q q, arg) + word = q arg q + } else { + # Use $'' quoting per Issue 8 + word = "$'" + for (i = 1; i <= length(arg); i++) { + char = substr(arg, i, 1) + if (char == "\\") + word = word "\\\\" + else if (char == q) + word = word "\\'" + else + ord = ord_by[char] + if (ord != "") + word = word "\\" sprintf("%03o", ord) + else + word = word char + } + word = word q + } + line = line word + if (arg_idx < argc - 1) line = line " " } + print line } EOF }