From: Eric Blake Date: Thu, 13 Nov 2008 04:45:42 +0000 (-0700) Subject: Add m4_chomp, m4_esyscmd_s. X-Git-Tag: v2.63b~87 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fa506f9116c42bba43e76d87334d177a8e6d8651;p=thirdparty%2Fautoconf.git Add m4_chomp, m4_esyscmd_s. * lib/m4sugar/m4sugar.m4 (m4_esyscmd_e, m4_chomp, m4_chomp_all): New macros. * doc/autoconf.texi (Redefined M4 Macros) : Document them. (Text processing Macros) : Likewise. * NEWS: Likewise. * tests/m4sugar.at (m4@&t@_esyscmd_s): New test. Signed-off-by: Eric Blake --- diff --git a/ChangeLog b/ChangeLog index 08bb49022..d2d2f7bcc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,14 @@ 2008-11-20 Eric Blake + Add m4_chomp, m4_esyscmd_s. + * lib/m4sugar/m4sugar.m4 (m4_esyscmd_e, m4_chomp, m4_chomp_all): + New macros. + * doc/autoconf.texi (Redefined M4 Macros) : Document + them. + (Text processing Macros) : Likewise. + * NEWS: Likewise. + * tests/m4sugar.at (m4@&t@_esyscmd_s): New test. + Remove _m4_index. * lib/m4sugar/m4sugar.m4 (_m4_index): Delete; it is more efficient to make callers guarantee a match. diff --git a/NEWS b/NEWS index 6ef072591..297b3ea5b 100644 --- a/NEWS +++ b/NEWS @@ -23,8 +23,8 @@ GNU Autoconf NEWS - User visible changes. `autoreconf -I dir' option. ** The following documented m4sugar macros are new: - m4_curry m4_default_quoted m4_map_args m4_map_args_pair - m4_set_map + m4_chomp m4_curry m4_default_quoted m4_esyscmd_s m4_map_args + m4_map_args_pair m4_set_map ** The following m4sugar macros are documented now: m4_copy m4_dumpdefs m4_rename diff --git a/doc/autoconf.texi b/doc/autoconf.texi index db3f7cc43..9e59d27aa 100644 --- a/doc/autoconf.texi +++ b/doc/autoconf.texi @@ -10440,6 +10440,14 @@ calls @code{m4_dumpdef} for all of the silently does nothing if @var{name} is undefined. @end defmac +@defmac m4_esyscmd_s (@var{command}) +@msindex{esyscmd_s} +Like @code{m4_esyscmd}, this macro expands to the result of running +@var{command} in a shell. The difference is that any trailing newlines +are removed, so that the output behaves more like shell command +substitution. +@end defmac + @defmac m4_exit (@var{exit-status}) @msindex{exit} This macro corresponds to @code{m4exit}. @@ -11392,6 +11400,17 @@ numbers @end example @end defmac +@defmac m4_chomp (@var{string}) +@defmacx m4_chomp_all (@var{string}) +@msindex{chomp} +@msindex{chomp_all} +Output @var{string} in quotes, but without a trailing newline. The +macro @code{m4_chomp} is slightly faster, and removes at most one +newline; the macro @code{m4_chomp_all} removes all consecutive trailing +newlines. Unlike @code{m4_flatten}, embedded newlines are left intact, +and backslash does not influence the result. +@end defmac + @defmac m4_combine (@ovar{separator}, @var{prefix-list}, @ovar{infix}, @ @var{suffix-1}, @ovar{suffix-2}, @dots{}) @msindex{combine} diff --git a/lib/m4sugar/m4sugar.m4 b/lib/m4sugar/m4sugar.m4 index 7671f0f5d..c9a7f9893 100644 --- a/lib/m4sugar/m4sugar.m4 +++ b/lib/m4sugar/m4sugar.m4 @@ -624,6 +624,13 @@ m4_define([m4_dumpdefs], [$#], [1], [m4_stack_foreach_lifo([$1], [m4_dumpdef([$1])m4_ignore])], [m4_map_args([$0], $@)])]) +# m4_esyscmd_s(COMMAND) +# --------------------- +# Like m4_esyscmd, except strip any trailing newlines, thus behaving +# more like shell command substitution. +m4_define([m4_esyscmd_s], +[m4_chomp_all(m4_esyscmd([$1]))]) + # m4_popdef(NAME) # --------------- @@ -822,6 +829,8 @@ m4_define([m4_echo], [$@]) # with balanced quotes (use quadrigraphs to get around this). The input # is not likely to have unbalanced -=<{(/)}>=- quotes, and it is possible # to have unbalanced (), provided it was specified with proper [] quotes. +# Likewise, ARG must either avoid unquoted comments, or must be sure +# to include the trailing newline to end the comment. # # Exploit that extra () will group unquoted commas and the following # whitespace, then convert () to []. m4_bpatsubst can't handle newlines @@ -2089,6 +2098,28 @@ m4_define([_m4_split], -=<{(]$3[)}>=-)]m4_changequote([, ])]) +# m4_chomp(STRING) +# m4_chomp_all(STRING) +# -------------------- +# Return STRING quoted, but without a trailing newline. m4_chomp +# removes at most one newline, while m4_chomp_all removes all +# consecutive trailing newlines. Embedded newlines are not touched, +# and a trailing backslash-newline leaves just a trailing backslash. +# +# m4_bregexp is slower than m4_index, and we don't always want to +# remove all newlines; hence the two variants. We massage characters +# to give a nicer pattern to match, particularly since m4_bregexp is +# line-oriented. Both versions must guarantee a match, to avoid bugs +# with precision -1 in m4_format in older m4. +m4_define([m4_chomp], +[m4_format([[%.*s]], m4_index(m4_translit([[$1]], [ +/.], [/ ])[./.], [/.]), [$1])]) + +m4_define([m4_chomp_all], +[m4_format([[%.*s]], m4_bregexp(m4_translit([[$1]], [ +/], [/ ]), [/*$]), [$1])]) + + # m4_flatten(STRING) # ------------------ # If STRING contains end of lines, replace them with spaces. If there diff --git a/tests/m4sugar.at b/tests/m4sugar.at index 5a90493ae..d1ee1fe6a 100644 --- a/tests/m4sugar.at +++ b/tests/m4sugar.at @@ -871,6 +871,34 @@ $1$#$@ AT_CLEANUP +## -------------- ## +## m4_esyscmd_s. ## +## -------------- ## + +AT_SETUP([m4@&t@_esyscmd_s]) +AT_KEYWORDS([m4@&t@_chomp m4@&t@_chomp_all]) + +AT_CHECK_M4SUGAR_TEXT( +[[m4_define([world], [WORLD])dnl +m4_chomp([abc]) +m4_chomp([world + +]) +m4_esyscmd_s([echo hello world]) +m4_esyscmd_s([echo '[goodbye, +cruel world + +]']) +]], [[abc +world + +hello WORLD +goodbye, +cruel world +]]) + +AT_CLEANUP + ## ---------- ## ## M4 Loops. ## ## ---------- ##