]> git.draconx.ca Git - dxcommon.git/blob - m4/cmdout.m4
5aa39253b397a67e2a3d9f6c2b4c47c661ba34ae
[dxcommon.git] / m4 / cmdout.m4
1 dnl Copyright © 2014 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 (with newlines
11 dnl converted to spaces), and action-if-ok is performed.  Otherwise, if
12 dnl the command is unsuccessful, then variable is not changed, and
13 dnl action-if-failed is performed.
14 AC_DEFUN([DX_COMMAND_OUTPUT], [AC_REQUIRE([_DX_COMMAND_OUTPUT_SETUP])dnl
15 AS_IF([$2 >conftest.out 2>&AS_MESSAGE_LOG_FD],
16         [m4_do([dx_fn_cmdout_collect_output $1],
17                 [m4_ifnblank([$3], [m4_newline([$3])])])],
18         [$4])
19 rm -f conftest.out
20 ])
21
22 AC_DEFUN_ONCE([_DX_COMMAND_OUTPUT_SETUP], [m4_divert_push([INIT_PREPARE])dnl
23 # Helper function to store the contents of conftest.out into a shell variable.
24 dx_fn_cmdout_collect_output () {
25   AS_UNSET([$][1])
26   exec 3<conftest.out
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], [" $_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()])