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) <m4_esyscmd_s>: Document
them.
(Text processing Macros) <m4_chomp>: Likewise.
* NEWS: Likewise.
* tests/m4sugar.at (m4@&t@_esyscmd_s): New test.

Signed-off-by: Eric Blake <ebb9@byu.net>
This commit is contained in:
Eric Blake 2008-11-12 21:45:42 -07:00
parent aa6c52d78a
commit fa506f9116
5 changed files with 89 additions and 2 deletions

View File

@ -1,5 +1,14 @@
2008-11-20 Eric Blake <ebb9@byu.net>
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) <m4_esyscmd_s>: Document
them.
(Text processing Macros) <m4_chomp>: 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.

4
NEWS
View File

@ -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

View File

@ -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}

View File

@ -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

View File

@ -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. ##
## ---------- ##