[PATCH 1/2] m4sh: Add _AS_INVOCATION

Eric Blake <[email protected]>
Newsgroups gmane.comp.sysutils.autoconf.patches,gmane.comp.sysutils.autoconf.bugs
Message-ID <[email protected]>
When outputting a script's command line to a log file, it helps
if we quote arguments suitable for re-pasting into a shell command
line.  Since we have more than one site (configure and config.status)
that weren't doing this, it helps if we first add an m4sh primitive
for easily creating this format, while minimizing forks and quoting
on arguments that don't need special handling.

For now, I've kept the macro undocumented; if there is demand, we
can promote it to the stable name AS_INVOCATION and add it to the
manual.

* lib/m4sugar/m4sh.m4 (_AS_INVOCATION): New macro.
* tests/m4sh.at (_AS@&t@_INVOCATION): Test it.
Inspired by a report by Kevin Brubeck Unhammer <[email protected]>.
Signed-off-by: Eric Blake <[email protected]>
---
 lib/m4sugar/m4sh.m4 | 14 ++++++++++++++
 tests/m4sh.at       | 23 +++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/lib/m4sugar/m4sh.m4 b/lib/m4sugar/m4sh.m4
index 3b37683..e0470ed 100644
--- a/lib/m4sugar/m4sh.m4
+++ b/lib/m4sugar/m4sh.m4
@@ -687,6 +687,20 @@ m4_defun([AS_UNSET],
 ## ------------------------------------------ ##


+# _AS_INVOCATION
+# --------------
+# Set $as_invocation to a quoted representation of '$ $0 $@' that can be
+# pasted into a shell command line to achieve the same invocation.
+m4_define([_AS_INVOCATION],
+[as_invocation='$'
+for as_arg in "$[0]" "$[@]"; do
+  case $as_arg in #((
+    *\'*) as_arg=\'`printf %s "$as_arg" | sed "s/'/'\\\\\\\\''/g"`\' ;;
+    '' | *[[!a-zA-Z0-9_.,:/+-]]*) as_arg=\'$as_arg\' ;;
+  esac
+  AS_VAR_APPEND([as_invocation], [" $as_arg"])
+done])
+
 # AS_MESSAGE_FD
 # -------------
 # Must expand to the fd where messages will be sent.  Defaults to 1,
diff --git a/tests/m4sh.at b/tests/m4sh.at
index c18cdcf..ff4f20f 100644
--- a/tests/m4sh.at
+++ b/tests/m4sh.at
@@ -1959,3 +1959,26 @@ AT_CHECK([$CONFIG_SHELL ./script], [], [foobar
 ])

 AT_CLEANUP
+
+
+## ---------------- ##
+## _AS_INVOCATION.  ##
+## ---------------- ##
+
+AT_SETUP([_AS@&t@_INVOCATION])
+
+AT_DATA_M4SH([script.as], [[dnl
+AS_INIT
+_AS_PREPARE
+_AS_INVOCATION
+echo "$as_invocation"
+]])
+
+AT_CHECK_M4SH
+AT_CHECK([$CONFIG_SHELL ./script], [], [$ ./script
+])
+AT_CHECK([$CONFIG_SHELL ./script a.1 "b  c" "d'e" 'f"g"' "" "#" "*" h=i],
+         [], [$ ./script a.1 'b  c' 'd'\''e' 'f"g"' '' '#' '*' 'h=i'
+])
+
+AT_CLEANUP
-- 
2.9.3
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.