@@ -32,13 +32,14 @@ dnl This macro may be invoked before `AC_INIT()`.
32
32
dnl
33
33
dnl Expansion type: shell code
34
34
dnl Requires: nothing
35
- dnl Version: 1.0.0
35
+ dnl Version: 1.0.1
36
36
dnl Author: madmurphy
37
37
dnl
38
38
dnl **************************************************************************
39
39
AC_DEFUN ( [ NS_SETVARS] ,
40
- [ AS_VAR_SET ( [ $1 ] , [ $2 ] ) ; m4_if ( m4_eval ( [ $# > 2] ) , [ 1] ,
41
- [ NS_SETVARS(m4_shift2 ( $@ ) )] ) ] )
40
+ [ m4_if ( [ $# ] , [ 0] , [ ] ,
41
+ [ AS_VAR_SET ( [ $1 ] , [ $2 ] ) [ ] m4_if ( [ $# ] , [ 1] , [ ] , [ $# ] , [ 2] , [ ] ,
42
+ [ m4_newline ( ) NS_SETVARS(m4_shift2 ( $@ ) )] ) ] ) ] )
42
43
43
44
44
45
dnl NS_GETVAR(var)
@@ -57,11 +58,12 @@ dnl This macro may be invoked before `AC_INIT()`.
57
58
dnl
58
59
dnl Expansion type: shell code
59
60
dnl Requires: nothing
60
- dnl Version: 1.0 .0
61
+ dnl Version: 1.1 .0
61
62
dnl Author: madmurphy
62
63
dnl
63
64
dnl **************************************************************************
64
- AC_DEFUN ( [ NS_GETVAR] , [ @S|@ {$1 }] )
65
+ AC_DEFUN ( [ NS_GETVAR] ,
66
+ [ AS_LITERAL_WORD_IF ( [ $1 ] , [ ${$1 }] , [ $(eval echo "\${$1 }")] ) ] )
65
67
66
68
67
69
dnl NS_GETOUT(command)
@@ -73,7 +75,7 @@ dnl Same as `$(command)`. This macro works exactly like `m4_esyscmd()`, but
73
75
dnl instead of executing a command during the `autoreconf` process, it
74
76
dnl executes it during the `configure` process, when all M4 macros have been
75
77
dnl already expanded. The returned value cannot therefore be stored in another
76
- dnl macro, but must be stored in a shell variable instead .
78
+ dnl macro, but must be stored in a shell variable.
77
79
dnl
78
80
dnl Example:
79
81
dnl
@@ -94,7 +96,7 @@ AC_DEFUN([NS_GETOUT], [@S|@@{:@$1@:}@])
94
96
dnl NS_UNSET(var1[, var2[, var3[, ... varN]]])
95
97
dnl **************************************************************************
96
98
dnl
97
- dnl Like `AS_UNSET()`, but it allows to unset many variables altogether
99
+ dnl Like `AS_UNSET()`, but allows to unset many variables altogether
98
100
dnl
99
101
dnl For example:
100
102
dnl
@@ -108,12 +110,14 @@ dnl This macro may be invoked before `AC_INIT()`.
108
110
dnl
109
111
dnl Expansion type: shell code
110
112
dnl Requires: nothing
111
- dnl Version: 1.0.0
113
+ dnl Version: 1.0.1
112
114
dnl Author: madmurphy
113
115
dnl
114
116
dnl **************************************************************************
115
117
AC_DEFUN ( [ NS_UNSET] ,
116
- [ m4_ifnblank ( [ $1 ] , [ AS_UNSET ( m4_quote ( m4_normalize ( [ $1 ] ) ) ) ;] ) m4_if ( [ $# ] , [ 1] , [ ] , [ NS_UNSET(m4_shift ( $@ ) )] ) ] )
118
+ [ m4_if ( [ $# ] , [ 0] , [ ] ,
119
+ [ AS_UNSET ( m4_normalize ( [ $1 ] ) ) ] ) [ ] m4_if ( [ $# ] , [ 1] , [ ] ,
120
+ [ m4_newline ( ) [ ] NS_UNSET(m4_shift ( $@ ) )] ) ] )
117
121
118
122
119
123
dnl NS_MOVEVAR(destination, source)
@@ -126,12 +130,12 @@ dnl This macro may be invoked before `AC_INIT()`.
126
130
dnl
127
131
dnl Expansion type: shell code
128
132
dnl Requires: nothing
129
- dnl Version: 1.0.0
133
+ dnl Version: 1.0.1
130
134
dnl Author: madmurphy
131
135
dnl
132
136
dnl **************************************************************************
133
137
AC_DEFUN ( [ NS_MOVEVAR] ,
134
- [ { AS_VAR_COPY ( [ $1 ] , [ $2 ] ) ; AS_UNSET ( [ $2 ] ) ; } ] )
138
+ [ AS_VAR_COPY ( [ $1 ] , [ $2 ] ) [ ] m4_newline ( ) [ ] AS_UNSET ( [ $2 ] ) ] )
135
139
136
140
137
141
dnl NS_REPLACEVAR(destination, source)
@@ -145,12 +149,14 @@ dnl This macro may be invoked before `AC_INIT()`.
145
149
dnl
146
150
dnl Expansion type: shell code
147
151
dnl Requires: nothing
148
- dnl Version: 1.0 .0
152
+ dnl Version: 1.1 .0
149
153
dnl Author: madmurphy
150
154
dnl
151
155
dnl **************************************************************************
152
156
AC_DEFUN ( [ NS_REPLACEVAR] ,
153
- [ { @S|@ {$2 +:} false && { AS_VAR_COPY ( [ $1 ] , [ $2 ] ) ; AS_UNSET ( [ $2 ] ) ; } || AS_UNSET ( [ $1 ] ) }] )
157
+ [ AS_VAR_SET_IF ( [ $2 ] ,
158
+ [ AS_VAR_COPY ( [ $1 ] , [ $2 ] ) [ ] m4_newline ( ) [ ] AS_UNSET ( [ $2 ] ) ] ,
159
+ [ AS_UNSET ( [ $1 ] ) ] ) ] )
154
160
155
161
156
162
dnl NS_IF(test1, run1[, test2, run2[, testN, runN]][, run-if-false])
@@ -828,11 +834,121 @@ AC_DEFUN([NS_HEREDOC],
828
834
[ { cat << m4_default_nblank_quoted ( [ $2 ] , [ NS_END_HEREDOC] ) [ ] m4_newline ( ) $1 [ ] m4_newline ( ) m4_default_nblank_quoted ( [ $2 ] , [ NS_END_HEREDOC] ) [ ] m4_newline ( ) }] )
829
835
830
836
831
-
837
+ dnl NS_CATCH(shell-code[, stdout-var-name[, stderr-var-name]])
838
+ dnl **************************************************************************
839
+ dnl
840
+ dnl Execute custom shell code and save `stdout` and `stderr` into shell
841
+ dnl variables
842
+ dnl
843
+ dnl Both `stdout-var-name` and `stderr-var-name` can be left empty, and in
844
+ dnl that case the respective file descriptor will be normally flushed.
845
+ dnl Alternatively, they can be set to a hyphen (`-`), and in that case the
846
+ dnl respective file descriptor will be redirected to `/dev/null`.
847
+ dnl
848
+ dnl A few examples:
849
+ dnl
850
+ dnl # Save both `stdout` and `stderr` into two variables
851
+ dnl NS_CATCH([
852
+ dnl echo 'This is stderr' 1>&2
853
+ dnl echo 'This is stdout'
854
+ dnl ], [MY_STDOUT], [MY_STDERR])
855
+ dnl
856
+ dnl # Save `stdout` into a variable and silence `stderr`
857
+ dnl NS_CATCH([
858
+ dnl echo 'This is stderr' 1>&2
859
+ dnl echo 'This is stdout'
860
+ dnl ], [MY_STDOUT], [-])
861
+ dnl
862
+ dnl # Save `stdout` into a variable and print `stderr` normally
863
+ dnl NS_CATCH([
864
+ dnl echo 'This is stderr' 1>&2
865
+ dnl echo 'This is stdout'
866
+ dnl ], [MY_STDOUT])
867
+ dnl
868
+ dnl # Save `stderr` into a variable and silence `stdout`
869
+ dnl NS_CATCH([
870
+ dnl echo 'This is stderr' 1>&2
871
+ dnl echo 'This is stdout'
872
+ dnl ], [-], [MY_STDERR])
873
+ dnl
874
+ dnl # Save `stderr` into a variable and print `stdout` normally
875
+ dnl NS_CATCH([
876
+ dnl echo 'This is stderr' 1>&2
877
+ dnl echo 'This is stdout'
878
+ dnl ], [], [MY_STDERR])
879
+ dnl
880
+ dnl # Silence both `stdout` and `stderr`
881
+ dnl NS_CATCH([
882
+ dnl echo 'This is stderr' 1>&2
883
+ dnl echo 'This is stdout'
884
+ dnl ], [-], [-])
885
+ dnl
886
+ dnl # Silence `stderr`, print only `stdout`
887
+ dnl NS_CATCH([
888
+ dnl echo 'This is stderr' 1>&2
889
+ dnl echo 'This is stdout'
890
+ dnl ], [], [-])
891
+ dnl
892
+ dnl # Silence `stdout`, print only `stderr`
893
+ dnl NS_CATCH([
894
+ dnl echo 'This is stderr' 1>&2
895
+ dnl echo 'This is stdout'
896
+ dnl ], [-])
897
+ dnl
898
+ dnl # Print both `stdout` and `stderr` normally
899
+ dnl NS_CATCH([
900
+ dnl echo 'This is stderr' 1>&2
901
+ dnl echo 'This is stdout'
902
+ dnl ])
903
+ dnl
904
+ dnl This macro may be invoked before `AC_INIT()`.
905
+ dnl
906
+ dnl NOTE: `NS_CATCH()` incorporates an answer of mine on stackoverflow
907
+ dnl (https://stackoverflow.com/a/59592881, "10. The POSIX-compliant
908
+ dnl version #2").
909
+ dnl
910
+ dnl Expansion type: shell code
911
+ dnl Requires: nothing
912
+ dnl Version: 1.0.0
913
+ dnl Author: madmurphy
914
+ dnl
832
915
dnl **************************************************************************
833
- dnl NOTE: The `NS_` prefix (which stands for "Not autoShell") is used with the
834
- dnl purpose of avoiding collisions with the default Autotools prefixes
835
- dnl `AC_`, `AM_`, `AS_`, `AX_`, `LT_`.
916
+ AC_DEFUN ( [ NS_CATCH] ,
917
+ [ m4_if ( [ $2$3 ] , [ ] ,
918
+ [ {m4_newline ( ) [ ] $1 [ ] m4_newline ( ) }] ,
919
+ [ $2$3 ] , [ --] ,
920
+ [ {m4_newline ( ) {m4_newline ( ) [ ] $1 [ ] m4_newline ( ) } 1> /dev/null 2>&1[ ] m4_newline ( ) }] ,
921
+ [ $2 ] , [ ] ,
922
+ [ m4_if ( [ $3 ] , [ -] ,
923
+ [ {m4_newline ( ) {m4_newline ( ) [ ] $1 [ ] m4_newline ( ) } 2> /dev/null[ ] m4_newline ( ) }] ,
924
+ [ {m4_newline ( ) [ ] AS_VAR_SET ( [ $3 ] , [ "$({] m4_newline ( ) [ $1 ] m4_newline ( ) [ } 3>&2 2>&1 1>&3 3>&-)"] ) [ ] m4_newline ( ) } 2>&1] ) ] ,
925
+ [ $2 ] , [ -] ,
926
+ [ m4_if ( [ $3 ] , [ ] ,
927
+ [ {m4_newline ( ) {m4_newline ( ) [ ] $1 [ ] m4_newline ( ) } 1> /dev/null[ ] m4_newline ( ) }] ,
928
+ [ {m4_newline ( ) [ ] AS_VAR_SET ( [ $3 ] , [ "$({] m4_newline ( ) [ $1 ] m4_newline ( ) [ } 3>&2 2>&1 1>&3 3>&-)"] ) [ ] m4_newline ( ) } 2> /dev/null] ) ] ,
929
+ [ $3 ] , [ ] ,
930
+ [ AS_VAR_SET ( [ $2 ] , [ "$({] m4_newline ( ) [ $1 ] m4_newline ( ) [ })"] ) ] ,
931
+ [ $3 ] , [ -] ,
932
+ [ AS_VAR_SET ( [ $2 ] , [ "$({] m4_newline ( ) [ $1 ] m4_newline ( ) [ } 2> /dev/null)"] ) ] ,
933
+ [ # See https://stackoverflow.com/a/59592881
934
+ __sep__=$'\cZ'
935
+ {
936
+
937
+ IFS=$'\n'"${__sep__}" read -r -d "${__sep__}" "$2 ";
938
+ IFS=$'\n'"${__sep__}" read -r -d "${__sep__}" "$3 ";
939
+ (IFS=$'\n'"${__sep__}" read -r -d "${__sep__}" __errno__; exit ${__errno__});
940
+ } <<__NA_EOF__
941
+ $((printf "${__sep__}%s${__sep__}%d${__sep__}" "$(((({ {m4_newline ( ) [ ] $1 [ ] m4_newline ( ) }m4_newline ( ) [ ] echo "${?}" 1>&3-; } | cut -z -d"${__sep__}" -f1 | tr -d '\0' 1>&4-) 4>&2- 2>&1- | cut -z -d"${__sep__}" -f1 | tr -d '\0' 1>&4-) 3>&1- | exit "$(cat)") 4>&1-)" "${?}" 1>&2) 2>&1)
942
+ __NA_EOF__
943
+ AS_UNSET ( [ __sep__] )
944
+ ] ) ] )
945
+
946
+
947
+
948
+ dnl **************************************************************************
949
+ dnl NOTE: The `NS_` prefix (which stands for "Not autoShell") is used with
950
+ dnl the purpose of avoiding collisions with the default Autotools
951
+ dnl prefixes `AC_`, `AM_`, `AS_`, `AX_`, `LT_`.
836
952
dnl **************************************************************************
837
953
838
954
0 commit comments