1 dnl Copyright © 2014-2015 Nick Bowler
3 dnl License WTFPL2: Do What The Fuck You Want To Public License, version 2.
4 dnl This is free software: you are free to do what the fuck you want to.
5 dnl There is NO WARRANTY, to the extent permitted by law.
7 dnl DX_COMMAND_OUTPUT(variable, command, [action-if-ok], [action-if-failed])
9 dnl Helper to capture standard output of a shell command. If the command
10 dnl is successful then variable is assigned with its output, and action-if-ok
11 dnl is performed. Otherwise, if the command is unsuccessful, then variable
12 dnl is not changed, and action-if-failed is performed.
13 AC_DEFUN([DX_COMMAND_OUTPUT], [AC_REQUIRE([_DX_COMMAND_OUTPUT_SETUP])dnl
14 AS_IF([$2 >conftest.do0 2>&AS_MESSAGE_LOG_FD],
15 [m4_do([dx_fn_cmdout_collect_output $1],
16 [m4_ifnblank([$3], [m4_newline([$3])])])],
18 rm -f conftest.do0 conftest.do1])
20 AC_DEFUN_ONCE([_DX_COMMAND_OUTPUT_SETUP], [m4_divert_push([INIT_PREPARE])dnl
21 # Helper function to store the contents of conftest.do0 into a shell variable.
22 dx_fn_cmdout_collect_output () {
24 # Double up backslashes as they will be stripped by read.
25 # Heirloom sh read apparently just eats backslashes at EOL, so we compromise
26 # by adding a marker that can be stripped out afterwards.
28 sed -e 's/\\/\\\\/g' -e 's/\\$/\\'"$_dx_eol/" conftest.do0 >conftest.do1 ||
32 while IFS=; read _dx_tmp <&3
35 # strip out EOL marker, if present
36 AS_CASE([$_dx_tmp], [*"\\$_dx_eol"],
37 [_dx_tmp=`AS_ECHO(["$_dx_tmp"]) | sed "s/$_dx_eol\$//"`])
39 [AS_VAR_APPEND([$][1], ["$as_nl$_dx_tmp"])],
40 [AS_VAR_SET([$][1], [$_dx_tmp])])