The 'read' builtin eats backslashes from the input, so this macro fails
to properly handle output containing backslashes. Since the -r option
is not portable according to the Autoconf manual, fix up the input by
escaping all backslashes.
While we're at it, remove the "feature" which converts newlines to
spaces because it seems rather silly.
-dnl Copyright © 2014 Nick Bowler
+dnl Copyright © 2014-2015 Nick Bowler
dnl
dnl License WTFPL2: Do What The Fuck You Want To Public License, version 2.
dnl This is free software: you are free to do what the fuck you want to.
dnl
dnl License WTFPL2: Do What The Fuck You Want To Public License, version 2.
dnl This is free software: you are free to do what the fuck you want to.
dnl DX_COMMAND_OUTPUT(variable, command, [action-if-ok], [action-if-failed])
dnl
dnl Helper to capture standard output of a shell command. If the command
dnl DX_COMMAND_OUTPUT(variable, command, [action-if-ok], [action-if-failed])
dnl
dnl Helper to capture standard output of a shell command. If the command
-dnl is successful then variable is assigned with its output (with newlines
-dnl converted to spaces), and action-if-ok is performed. Otherwise, if
-dnl the command is unsuccessful, then variable is not changed, and
-dnl action-if-failed is performed.
+dnl is successful then variable is assigned with its output, and action-if-ok
+dnl is performed. Otherwise, if the command is unsuccessful, then variable
+dnl is not changed, and action-if-failed is performed.
AC_DEFUN([DX_COMMAND_OUTPUT], [AC_REQUIRE([_DX_COMMAND_OUTPUT_SETUP])dnl
AC_DEFUN([DX_COMMAND_OUTPUT], [AC_REQUIRE([_DX_COMMAND_OUTPUT_SETUP])dnl
-AS_IF([$2 >conftest.out 2>&AS_MESSAGE_LOG_FD],
- [m4_do([dx_fn_cmdout_collect_output $1],
- [m4_ifnblank([$3], [m4_newline([$3])])])],
- [$4])
-rm -f conftest.out
-])
+AS_IF([$2 >conftest.do0 2>&AS_MESSAGE_LOG_FD],
+ [m4_do([dx_fn_cmdout_collect_output $1],
+ [m4_ifnblank([$3], [m4_newline([$3])])])],
+ [$4])
+rm -f conftest.do0 conftest.do1])
AC_DEFUN_ONCE([_DX_COMMAND_OUTPUT_SETUP], [m4_divert_push([INIT_PREPARE])dnl
AC_DEFUN_ONCE([_DX_COMMAND_OUTPUT_SETUP], [m4_divert_push([INIT_PREPARE])dnl
-# Helper function to store the contents of conftest.out into a shell variable.
+# Helper function to store the contents of conftest.do0 into a shell variable.
dx_fn_cmdout_collect_output () {
AS_UNSET([$][1])
dx_fn_cmdout_collect_output () {
AS_UNSET([$][1])
+ # Double up backslashes as they will be stripped by read.
+ sed 's/\\/\\\\/g' conftest.do0 >conftest.do1 || return
+ exec 3<conftest.do1
_dx_save_IFS=$IFS
IFS=
while read _dx_tmp <&3
do
_dx_save_IFS=$IFS
IFS=
while read _dx_tmp <&3
do
-AS_VAR_SET_IF([$][1],
- [AS_VAR_APPEND([$][1], [" $_dx_tmp"])],
- [AS_VAR_SET([$][1], [$_dx_tmp])])
+ AS_VAR_SET_IF([$][1],
+ [AS_VAR_APPEND([$][1], ["$as_nl$_dx_tmp"])],
+ [AS_VAR_SET([$][1], [$_dx_tmp])])
done
IFS=$_dx_save_IFS
exec 3<&-
done
IFS=$_dx_save_IFS
exec 3<&-
TEST_CONFIGURE
AT_CLEANUP
TEST_CONFIGURE
AT_CLEANUP
+
+AT_SETUP([DX_COMMAND_OUTPUT])
+
+AT_DATA([expout],
+[[ leading whitespace
+trailing whitespace @&t@
+embedded\ backslashes
+ @&t@
+backslash\
+newline
+]])
+
+TEST_CONFIGURE_AC(
+[[DX_COMMAND_OUTPUT([var], [cat expout])
+AS@&t@_ECHO(["$var"]) >test.out
+]])
+TEST_AUTORECONF
+TEST_CONFIGURE
+
+AT_CHECK([cat test.out], [0], [expout])
+
+AT_CLEANUP