]> git.draconx.ca Git - dxcommon.git/blob - m4/cmdout.m4
Fix DX_COMMAND_OUTPUT backslash-newline on heirloom-sh.
[dxcommon.git] / m4 / cmdout.m4
1 dnl Copyright © 2014-2015 Nick Bowler
2 dnl
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.
6
7 dnl DX_COMMAND_OUTPUT(variable, command, [action-if-ok], [action-if-failed])
8 dnl
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])])])],
17   [$4])
18 rm -f conftest.do0 conftest.do1])
19
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 () {
23   AS_UNSET([$][1])
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.
27   _dx_eol='@%eol@%'
28   sed -e 's/\\/\\\\/g' -e 's/\\$/\\'"$_dx_eol/" conftest.do0 >conftest.do1 ||
29     return
30   exec 3<conftest.do1
31   _dx_save_IFS=$IFS
32   while IFS=; read _dx_tmp <&3
33   do
34     IFS=$_dx_save_IFS
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\$//"`])
38     AS_VAR_SET_IF([$][1],
39       [AS_VAR_APPEND([$][1], ["$as_nl$_dx_tmp"])],
40       [AS_VAR_SET([$][1], [$_dx_tmp])])
41   done
42   IFS=$_dx_save_IFS
43   exec 3<&-
44 }
45
46 m4_divert_pop()])