]> git.draconx.ca Git - dxcommon.git/blob - m4/cmdout.m4
Update DX_COMMAND_OUTPUT to handle newlines and backslashes.
[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   sed 's/\\/\\\\/g' conftest.do0 >conftest.do1 || return
26   exec 3<conftest.do1
27   _dx_save_IFS=$IFS
28   IFS=
29   while read _dx_tmp <&3
30   do
31     AS_VAR_SET_IF([$][1],
32       [AS_VAR_APPEND([$][1], ["$as_nl$_dx_tmp"])],
33       [AS_VAR_SET([$][1], [$_dx_tmp])])
34   done
35   IFS=$_dx_save_IFS
36   exec 3<&-
37 }
38
39 m4_divert_pop()])